当有闹钟时,BroadcastReceiver不会触发
问题描述:
我正在开发一个应用程序,当闹钟被触发时,它会在后台执行某些操作。现在它只是一个敬酒。 运行时,我没有得到任何错误,但当警报响起时,广播接收器不会被调用。 我错过了什么?当有闹钟时,BroadcastReceiver不会触发
AlarmReceiver:
public class AlarmReceiver extends BroadcastReceiver {
public static final String ALARM_ALERT_ACTION = "com.android.deskclock.ALARM_ALERT";
@Override
public void onReceive(Context context, Intent intent) {
{
Intent alarm = new Intent(ALARM_ALERT_ACTION);
// context.registerReceiver(this, alarm);
String action = intent.getAction();
if (action.equals(ALARM_ALERT_ACTION))
{
// for play/pause mediaplayer
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
}
}
}
AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.noel.alarmnotification">
<uses-permission android:name="com.android.alarm.permission."/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
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>
<receiver android:name=".AlarmReceiver" android:process=":remote">
<intent-filter>
<action android:name="com.android.deskclock.ALARM_ALERT"/>
</intent-filter>
</receiver>
</activity>
</application>
</manifest>
EDIT
MainActivity:
public class MainActivity extends AppCompatActivity {
public static final String ALARM_ALERT_ACTION = "com.android.deskclock.ALARM_ALERT";
public static final String ALARM_SNOOZE_ACTION = "com.android.deskclock.ALARM_SNOOZE";
public static final String ALARM_DISMISS_ACTION = "com.android.deskclock.ALARM_DISMISS";
public static final String ALARM_DONE_ACTION = "com.android.deskclock.ALARM_DONE";
private BroadcastReceiver mReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
if (action.equals(ALARM_ALERT_ACTION) || action.equals(ALARM_DISMISS_ACTION) || action.equals(ALARM_SNOOZE_ACTION) || action.equals(ALARM_DONE_ACTION))
{
// for play/pause mediaplayer
}
}
};
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
IntentFilter filter = new IntentFilter(ALARM_ALERT_ACTION);
filter.addAction(ALARM_DISMISS_ACTION);
filter.addAction(ALARM_SNOOZE_ACTION);
filter.addAction(ALARM_DONE_ACTION);
registerReceiver(mReceiver, filter);
}
}
瓦时恩我编程方式创建一个寄存器,一旦应用程序被关闭,它不广播报警
当你听到广播接收器没有被叫做***时,你的意思是什么?您会听到警报,但接收器内的方法不会被执行? – Opiatefuchs
是的,这就是发生了什么,我调试它,但程序永远不会到达广播接收器内的断点 – programernoob
我不太确定,但我想你需要的权限''。如果你是在棉花糖行事,你也需要运行时的权限请求。 – Opiatefuchs