内部类广播接收者无法接收adb发送的广播

Lion1946 2018-01-26 09:54:08
如题,内部类广播接收者,通过代码的方式发送广播时,可以被接收到,但是在cmd中通过adb shell发送相同广播时,无法接收到,是什么问题?

MainActivity.java

public class MainActivity extends Activity {
private Button startBtn;
private Button stopBtn;
private ActivityReceiver activityReceiver;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
//初始化控件
init();
//创建进程
inProcess();
startBtn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Intent intent = new Intent(context, PGService.MsgReceiver.class); //此处发送广播可以被接收到
intent.setAction("com.pgauto.setupsensor");
sendBroadcast(intent);
state = 1;
if(state == 1){
editText.setText("Running");
}else if(state == 0 || !isServiceRunning(context, "com.pgauto.PGService")){
editText.setText("Stopped");
}
}
});

stopBtn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Intent intent = new Intent(context, PGService.MsgReceiver.class); //此处发送广播可以被接收到
intent.setAction("com.pgauto.teardownsensor");
sendBroadcast(intent);
}
});
}

//初始化控件函数
private void init() {
startBtn=(Button)findViewById(R.id.openBtn);
stopBtn=(Button) findViewById(R.id.closeBtn);
}

//进程函数
private void inProcess(){
Intent intent =new Intent(MainActivity.this, PGService.class);
startService(intent);
}

@Override
protected void onStart() {
super.onStart();
}

protected void onResume() {
super.onResume();
}

public static class ActivityReceiver extends BroadcastReceiver { //该内部类广播接收者
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle=intent.getExtras();
String msg = intent.getAction();
}
}
}

AndroidManifest.xml文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pgauto">

<application
android:name=".AndroidApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<receiver android:name=".PGReceiver" >
<intent-filter>
<action android:name="com.pgauto.action" />
</intent-filter>
</receiver>
<receiver android:name=".MainActivity$ActivityReceiver" >
<intent-filter>
<action android:name="com.pgauto.activityreceiver" />
</intent-filter>
</receiver>
<service android:name=".PGService"></service>
</application>

</manifest>

CMD中发送广播不能被接收到:
adb shell am broadcast -a com.pgauto.setupsensor
adb shell am broadcast -a com.pgauto.teardownsensor
...全文
439 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧