我有一个带有broadcastreceiver的活动和receiver.class。我想从mainactivity启动接收器,但我不能用我的解决方案做到这一点。
这可能是
Intent iinent= new Intent(MainActivity.this,MyReceiver.class);
startActivity(iinent);
当我尝试这个应用程序时,我说不幸的是应用程序停止了''我的logcat说这个意图'线路不正确。
还有其他办法吗?
答案 0 :(得分:0)
用于发送广播接收器广播接收器必须注册
<receiver android:name="TestReceiver">
<intent-filter>
<action android:name="ACTION_NAME"/>
</intent-filter>
</receiver>
活动
IntentFilter filter = new IntentFilter();
filter.addAction("ACTION_NAME");
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//do something based on the intent's action
}
}
registerReceiver(receiver, filter);
SendBroadcast
Intent intnet = new Intent("ACTION_NAME");
sendBroadcast(intnet);