内部类广播接收者无法接收adb发送的广播
如题,内部类广播接收者,通过代码的方式发送广播时,可以被接收到,但是在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