我是一名新手,正在学习Android开发。我尝试在我的活动中编写代码来注册广播接收器。以下是我的代码:
MyReciever类:
public class MyReciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("===>", "onReceive: "+ intent.getAction());
Toast.makeText(context, "I got it "+ intent.getIntExtra("MyValue",0), Toast.LENGTH_SHORT).show();
}
}
我的活动:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myReciever = new MyReciever();
intentFilter = new IntentFilter();
intentFilter.addAction("test");
}
@Override
protected void onResume() {
registerReceiver(myReciever, intentFilter);
super.onResume();
}
}
清单:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.safarayaneh.mybroadcastreciever">
<application
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>
</application>
</manifest>
当我运行我的应用程序时,没有任何反应,也没有toast弹出!我已阅读此文章和此文章,但我不明白我的问题出在哪里。
"android.intent.action.TIME_TICK"
广播,该广播由系统每分钟发送一次。没有任何东西发送带有您自定义操作"test"
的广播。 - Mike M.