android 之 Intent、broadcast
Intent的功能有:
在mainActivity中為按鈕1添加監聽事件:
listener1 = new OnClickListener() {
??? @Override
??? public void onClick(View v) {
??????? // TODO Auto-generated method stub
???????Intent intent1 = new Intent(mainActivity.this, Activity1.class);
??????? intent1.putExtra("mainActivity", "這是來自mainActivity的數據");
??????? startActivityForResult(intent1,?REQUEST_CODE);
??? }
};
在Activity1中接收來自mainActivity中Intent中的數據:
String data = null;
Bundle extras =?getIntent().getExtras();
if (extras != null) {
??? data = extras.getString("mainActivity");
}
setTitle("現在在Activity1里:" + data);
為Activity1中的按鈕添加監聽事件,返回一個Intent:
listener1 = new OnClickListener() {
??????????? @Override
??????????? public void onClick(View v) {
??????????????? // TODO Auto-generated method stub
??????????????? Bundle bundle = new Bundle();
??????????????? bundle.putString("store", "數據來自Activity1");
??????????????? Intent mIntent = new Intent();
??????????????? mIntent.putExtras(bundle);
????????????????setResult(RESULT_OK, mIntent);
??????????????? finish();
??????????? }
??????? };
在mainActivity中覆寫onActivityResult()方法,對返回的內容處理:
@Override
??? protected void?onActivityResult(int?requestCode, int?resultCode, Intent data) {
??????? // TODO Auto-generated method stub
??????? super.onActivityResult(requestCode, resultCode, data);
????????if (requestCode ==?REQUEST_CODE) {
??????????? if (resultCode ==?RESULT_CANCELED) {
??????????????? setTitle("取消");
??????????? } else if (resultCode ==?RESULT_OK) {
??????????????? String temp = null;
??????????????? Bundle extras =?data.getExtras();
??????????????? if (extras != null) {
??????????????????? temp = extras.getString("store");
??????????????? }
??????????????? setTitle("在mainActivity中:"+temp);
??????????? }
??????? }
??? }
為按鈕2添加監聽事件:
protected static final String?ACTION1?= "com.sunny.action.BROADCASE";
?
listener2 = new OnClickListener() {
??????????? @Override
??????????? public void onClick(View v) {
??????????????? // TODO Auto-generated method stub
????????????????Intent intent2 = new Intent(ACTION1);
??????????????? sendBroadcast(intent2);
??????????? }
??????? };
添加一個Broadcast Receiver,其捕獲action為com.sunny.action.BROADCASE的Intent,生成Notification:
public class?broadcastReceive1?extends BroadcastReceiver {
??? private static final int NOTIFICATION_ID = 0;
??? Context context;
????
??? @Override
??? public void?onReceive(Context context, Intent intent) {
??????? // TODO Auto-generated method stub
??????? this.context=context;
???????showNotification();
??? }??? private void showNotification() {
??????? // TODO Auto-generated method stub
??????? NotificationManager notificationManager=(NotificationManager) context.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
??????? Notification notification=new Notification(R.drawable.icon, "在broadcastReceive1中",System.currentTimeMillis());
??????? PendingIntent contentIntent=PendingIntent.getActivity(context, 0, new Intent(context,mainActivity.class), 0);
??????? notification.setLatestEventInfo(context, "在broadcastReceive1中:", null, contentIntent);
??????? notificationManager.notify(NOTIFICATION_ID, notification);
??? }}
其在AndroidManifest.xml中注冊:
<receiver android:name=".broadcastReceive1">
??? <intent-filter>
??????? <action android:name="com.sunny.action.BROADCASE" />
??? </intent-filter>
</receiver>
Intent的功能有:
在mainActivity中為按鈕1添加監聽事件:
listener1 = new OnClickListener() {
??? @Override
??? public void onClick(View v) {
??????? // TODO Auto-generated method stub
???????Intent intent1 = new Intent(mainActivity.this, Activity1.class);
??????? intent1.putExtra("mainActivity", "這是來自mainActivity的數據");
??????? startActivityForResult(intent1,?REQUEST_CODE);
??? }
};
在Activity1中接收來自mainActivity中Intent中的數據:
String data = null;
Bundle extras =?getIntent().getExtras();
if (extras != null) {
??? data = extras.getString("mainActivity");
}
setTitle("現在在Activity1里:" + data);
為Activity1中的按鈕添加監聽事件,返回一個Intent:
listener1 = new OnClickListener() {
??????????? @Override
??????????? public void onClick(View v) {
??????????????? // TODO Auto-generated method stub
??????????????? Bundle bundle = new Bundle();
??????????????? bundle.putString("store", "數據來自Activity1");
??????????????? Intent mIntent = new Intent();
??????????????? mIntent.putExtras(bundle);
????????????????setResult(RESULT_OK, mIntent);
??????????????? finish();
??????????? }
??????? };
在mainActivity中覆寫onActivityResult()方法,對返回的內容處理:
@Override
??? protected void?onActivityResult(int?requestCode, int?resultCode, Intent data) {
??????? // TODO Auto-generated method stub
??????? super.onActivityResult(requestCode, resultCode, data);
????????if (requestCode ==?REQUEST_CODE) {
??????????? if (resultCode ==?RESULT_CANCELED) {
??????????????? setTitle("取消");
??????????? } else if (resultCode ==?RESULT_OK) {
??????????????? String temp = null;
??????????????? Bundle extras =?data.getExtras();
??????????????? if (extras != null) {
??????????????????? temp = extras.getString("store");
??????????????? }
??????????????? setTitle("在mainActivity中:"+temp);
??????????? }
??????? }
??? }
為按鈕2添加監聽事件:
protected static final String?ACTION1?= "com.sunny.action.BROADCASE";
?
listener2 = new OnClickListener() {
??????????? @Override
??????????? public void onClick(View v) {
??????????????? // TODO Auto-generated method stub
????????????????Intent intent2 = new Intent(ACTION1);
??????????????? sendBroadcast(intent2);
??????????? }
??????? };
添加一個Broadcast Receiver,其捕獲action為com.sunny.action.BROADCASE的Intent,生成Notification:
public class?broadcastReceive1?extends BroadcastReceiver {
??? private static final int NOTIFICATION_ID = 0;
??? Context context;
????
??? @Override
??? public void?onReceive(Context context, Intent intent) {
??????? // TODO Auto-generated method stub
??????? this.context=context;
???????showNotification();
??? }??? private void showNotification() {
??????? // TODO Auto-generated method stub
??????? NotificationManager notificationManager=(NotificationManager) context.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
??????? Notification notification=new Notification(R.drawable.icon, "在broadcastReceive1中",System.currentTimeMillis());
??????? PendingIntent contentIntent=PendingIntent.getActivity(context, 0, new Intent(context,mainActivity.class), 0);
??????? notification.setLatestEventInfo(context, "在broadcastReceive1中:", null, contentIntent);
??????? notificationManager.notify(NOTIFICATION_ID, notification);
??? }}
其在AndroidManifest.xml中注冊:
<receiver android:name=".broadcastReceive1">
??? <intent-filter>
??????? <action android:name="com.sunny.action.BROADCASE" />
??? </intent-filter>
</receiver>
轉載于:https://www.cnblogs.com/yechanglv/p/6922954.html
總結
以上是生活随笔為你收集整理的android 之 Intent、broadcast的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JS简单实现自定义右键菜单
- 下一篇: 菲字开头的成语有哪些?