android ComponentName 示例

public class MainActivity extends Activity {

	private NetworkReceiver connectionReceiver = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		// 连接性管理 android.net

		// 动态注册 广播接收器
		IntentFilter intentFilter = new IntentFilter();
		intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
		connectionReceiver = new NetworkReceiver();
		registerReceiver(connectionReceiver, intentFilter);

		//
		if (connectionReceiver.isConnected()) {

		} else {
			runOnUiThread(new Runnable() {
				@Override
				public void run() {
					AlertDialog.Builder builder = new AlertDialog.Builder(
							MainActivity.this);
					builder.setIcon(R.drawable.ic_launcher);
					builder.setTitle("网络错误");
					builder.setMessage("你确定要去设置网络么 ?");
					builder.setPositiveButton("不了", null);
					builder.setNegativeButton("确定",
							new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog,
								int which) {
							Intent intent = null;
							try {
								String sdkVersion = android.os.Build.VERSION.SDK;
								if (Integer.valueOf(sdkVersion) > 10) {
									intent = new Intent(
											android.provider.Settings.ACTION_WIRELESS_SETTINGS);
								} else {
									intent = new Intent();
									//组件名称,第一个参数是包名,也是主配置文件Manifest里设置好的包名 
									//第二个参数 是全类名
									ComponentName comp = new ComponentName(
											"com.android.settings",
											"com.android.settings.WirelessSettings");
									intent.setComponent(comp);
									intent.setAction("android.intent.action.VIEW");
								}
								MainActivity.this.startActivity(intent);
							} catch (Exception e) {
								Log.w("TAG",
										"open network settings failed, please check...");
								e.printStackTrace();
							}
						}
					});
					builder.show();
				}
			});
		}

	}

 

@Override
	protected void onDestroy() {
		Log.d("TAG", "onDestroy");
		if (connectionReceiver != null) {
			unregisterReceiver(connectionReceiver);
			Log.d("TAG", "unregisterReceiver Success!");
		}
	}

 
android ComponentName 示例_第1张图片
 
android ComponentName 示例_第2张图片
 

你可能感兴趣的:(android)