android 之 Intent、broadcast

來源:互聯網
上載者:User

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>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.