編輯:關於Android編程
首先, 需要在AndroidManifest.xml 中添加下列權限:
package com.akm.detectinternetconnectionstatus; import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; public class ConnectionDetector { private Context _context; public ConnectionDetector(Context context) { this._context = context; } public boolean isConnectingToInternet() { ConnectivityManager connectivity = (ConnectivityManager) _context .getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivity != null) { NetworkInfo[] info = connectivity.getAllNetworkInfo(); if (info != null) for (int i = 0; i < info.length; i++) if (info[i].getState() == NetworkInfo.State.CONNECTED) { return true; } } return false; } }
示例:
package com.akm.detectinternetconnectionstatus; import android.os.Bundle; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.view.Menu; import android.view.MenuInflater; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { ConnectionDetector cd; Boolean isInternetPresent = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btnStatus = (Button) findViewById(R.id.btn_check); cd = new ConnectionDetector(getApplicationContext()); btnStatus.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // get Internet status isInternetPresent = cd.isConnectingToInternet(); // check for Internet status if (isInternetPresent) { // Internet Connection is Present // make HTTP requests showAlertDialog(MainActivity.this, "Internet Connection", "You have internet connection", true); } else { // Internet connection is not present // Ask user to connect to Internet showAlertDialog(MainActivity.this, "No Internet Connection", "You don’t have internet connection.", false); } } }); } /** * Function to display simple Alert Dialog * * @param context * – application context * @param title * – alert dialog title * @param message * – alert message * @param status * – success/failure (used to set icon) * */ public void showAlertDialog(Context context, String title, String message, Boolean status) { AlertDialog alertDialog = new AlertDialog.Builder(context).create(); // Setting Dialog Title alertDialog.setTitle(title); // Setting Dialog Message alertDialog.setMessage(message); // Setting alert dialog icon alertDialog.setIcon(R.drawable.ic_launcher); // alertDialog.setIcon((status) ? R.drawable.success : R.drawable.fail); // Setting OK Button alertDialog.setButton(getString(R.string.ok), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { } }); // Showing Alert Message alertDialog.show(); } }
在一篇博客中看到一篇文章,感覺描述的還可以:在前面的博客中介紹的都是使用java開發Android應用,這篇博客將介紹java通過使用jni調用c語言做開發為了更加形象的
下面是配置Android開發ADB環境變量的操作步驟。工具/原料win7系統電腦+Android SDK方法/步驟1.首先右擊計算機——屬性——高級系統設置——環境變量;
Android 5.0 Lollipop 推出了 Material Design 安卓界面風格的設計理念並且給出了一個 Material Design 的兼容庫,庫中包含
讓你的apk文件盡可能小,應該使移除未使用的代碼和資源文件。那麼本章節介紹了如何做到讓APK更小,性能更好,下載轉化率會更高,以及如何指定在構建APK過程中保留或移除的代