本文着重解决问题: 1 响应notification事件 2 实现获取notification内数据思路 注意:manifest文件与主体文件中的绿色部分必须匹配,即获取消息后,根据action来判断是否为自己所需要的数据。 关于实现获取notification内数据的思路: 1) 在Activity 里注册接收广播,能直接设置TextView 缺点是 只有启动Activity 才能收到 2) 在Activity里注册全局广播 <receiver android:name=".SMSActivity$ReceiverSMS" > <intent-filter> <action android:name="android.provider.Telephony.SMS_RECEIVED" /> </intent-filter> </receiver> 3) 如你所写,在 onReceive创建本地储存(数据库等),在 Activity读取。 4) 在onReceive结束后,发送广播,在Activity接收。 /* manifest文件 */ <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.mybroadcastpackage" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <receiver android:name="com.example.mybroadcastpackage.MyReceiver"> <intent-filter> <action android:name="www.liaoyuhuan.com"/> </intent-filter> </receiver> <activity android:name="com.example.mybroadcastpackage.MyBroadcastActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> /* 布局文件: */ <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/id_btSend" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/_send_broadcast" /> <Button android:id="@+id/id_btReceive" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/id_btSend" android:text="@string/_receive_broadcast" /> <Button android:id="@+id/id_btClear" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/id_btReceive" android:text="@string/_clear_broadcast" /> </RelativeLayout> /* 事件类的主体 */ package com.example.mybroadcastpackage; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.Toast; import android.app.Activity; import android.content.Intent; public class MyBroadcastActivity extends Activity { Button m_btSend,m_btRecv,m_btClear; private final int CLICK_SEND = 1; private final int CLICK_RECV = 2; private final int CLICK_CLEAR = 3; //private ScreenActionReceiver m_tScreenActionReceiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my_broadcast); BindButtonListenerWithId(m_btSend,R.id.id_btSend,CLICK_SEND); BindButtonListenerWithId(m_btRecv,R.id.id_btReceive,CLICK_RECV); BindButtonListenerWithId(m_btClear,R.id.id_btClear,CLICK_CLEAR); } private void BindButtonListenerWithId(Button button,int nID,int nTagID) { button = (Button)findViewById(nID); button.setOnClickListener(new ClickButtonListener()); button.setTag(nTagID); } private final class ClickButtonListener implements View.OnClickListener { @Override public void onClick(View view) { switch((Integer) view.getTag()) { case CLICK_SEND: Log.w("CLICK_SEND", "点击按键"); Intent intent = new Intent("forBroadcast"); intent.setAction("www.liaoyuhuan.com"); intent.putExtra("name", "this is broadcast test!"); sendBroadcast(intent);//发送广播 Toast.makeText(getApplicationContext(), "发送广播成功", Toast.LENGTH_SHORT).show(); break; case CLICK_RECV: Log.w("CLICK_RECV", "点击按键"); break; case CLICK_CLEAR: Log.w("CLICK_CLEAR", "点击按键"); break; default: break; } } } } /* 响应该消息,并获取消息中的数据 */ package com.example.mybroadcastpackage; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; public class MyReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String name = intent.getExtras().getString("name"); Log.w("Recevier1", "接收到:"+name); } }