android BroadcastReceiver 不启动



我正试图使用broadcastreceiver与警报管理器,但不幸的是它不工作在我的手机(android 10)。broadcastReceiver没有初始化。如果有人帮助我,我会感激他/她

<<p>BroadcastReceiver类/strong>
`public class Alarm extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"Wake up",Toast.LENGTH_LONG).show();
Log.i("fffffgggg", "onReceive: reached broadcast");
MediaPlayer mediaPlayer=MediaPlayer.create(context, Settings.System.DEFAULT_RINGTONE_URI);
mediaPlayer.start();
}`

货单

<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>
<receiver android:name=".Alarm" android:exported="true" android:enabled="true">
</receiver>
</application>
<<p>MainActivity类/strong>
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent notifyintent = new Intent(MainActivity.this, Alarm.class);
PendingIntent pIntent = PendingIntent.getBroadcast(MainActivity.this, 0, notifyintent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
//update 30 seconds
alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),30*1000, pIntent);
}

你需要决定一个动作(一些字符串),配置你的广播接收器来处理这个动作,并创建一个pending intent来触发这个动作。

是这样的。

清单:

<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>
<receiver android:name=".Alarm" android:exported="true" android:enabled="true">
<intent-filter>
<action android:name="MY_ACTION" />
</intent-filter>
</receiver>
</application>
主要活动:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent notifyintent = new Intent("MY_ACTION");
PendingIntent pIntent = PendingIntent.getBroadcast(MainActivity.this, 0, notifyintent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
//update 30 seconds
alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),30*1000, pIntent);
}

此外,建议确保您在onReceive函数中获得正确的操作。

AlarmManager使用Pending Intent作为基础。Pending intent是一个intent修改,所以首先你必须在manifest中定义action,并把这个值赋给Pending intent。要了解更多细节,您可以阅读警报管理器谷歌文档,我希望我有帮助。

相关内容

  • 没有找到相关文章

最新更新