当我点击main.xml上的按钮
时,我试图重新接收BroadcastReceiver **mainfest.xml**
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".boardCast">
<intent-filter>
<action android:name="borad.cast"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
上面的清单文件接收器注册我们自己的动作,如android.action =“broad.cast”
**mainActivty.java**
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button samll = (Button)findViewById(R.id.button1);
samll.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent ii = new Intent(getApplicationContext(),boardCast.class);
PendingIntent pp = PendingIntent.getBroadcast(getApplicationContext(), 0, ii, 0);
}
});
}
上面的Activty类cantain按钮,当我点击那个按钮时,BroadcastReceiver应该触发 boardcast.java
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if(intent.getAction().equals("borad.cast")){
Toast.makeText(context, "sample", Toast.LENGTH_LONG).show();
}
}
答案 0 :(得分:3)
摆脱未决意图并以这种方式执行:
点击事件调用的onclick:
public void onClick(View v) {
// TODO Auto-generated method stub
broadcastMsg("borad.cast");
}
});
//这里是函数定义:
public void broadcastMsg(String intentName) {
final Intent intent = new Intent(intentName);
sendBroadcast(intent);
}