我的任务是设置闹钟以触发接收器在特定时间显示通知。
如果应用程序被打开或只是在后台,代码可以正常运行。
在应用程序被杀死时,仍然可以在执行警报时触发接收器。
但是,没有显示任何通知。
我该怎么办?
闹钟功能:
public static void makeAlarm(Context context,Date date) {
Log.i("Alarm", "makeAlarm");
AlarmManager alarmMgr;
PendingIntent alarmIntent;
alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
alarmMgr.set(AlarmManager.RTC_WAKEUP,
date.getTime(), alarmIntent);
}
AlarmReceiver
public class AlarmReceiver extends BroadcastReceiver {
public AlarmReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
Log.i("Alarm", "onReceive");
android.support.v4.app.NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.app_logo)
.setContentTitle("Title")
.setContentText("Message");
Intent resultIntent = new Intent(context, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Random rand = new Random();
mNotificationManager.notify(rand.nextInt(), mBuilder.build());
}
}
的Manifest.xml
<receiver
android:name=".receiver.AlarmReceiver"
android:enabled="true"
android:exported="true" />
答案 0 :(得分:1)
请运行示例代码。我认为它可以按你的意愿工作。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUpAlarm(10 * 1000);
}
public void setUpAlarm(long triggerTimeInMS) {
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1, intent, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+triggerTimeInMS , pendingIntent);
}
}
创建接收器类 -
public class AlarmReceiver extends BroadcastReceiver {
private String TAG = "AlarmLog";
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Incoming Call Received", Toast.LENGTH_LONG).show();
showNotification(context);
}
private void showNotification(Context context) {
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.alarm)
.setContentTitle("Notification!") // title for notification
.setContentText("Alarm Received")
.setSound(soundUri)// message for notification
.setAutoCancel(true); // clear notification after click
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
}
}
不要忘记注册接收者。在manifest.xml里面放了下面的代码 -
<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>
</activity>
<receiver android:name="com.example.shajib.alarmtest.AlarmReceiver" />
</application>
答案 1 :(得分:0)
使用 -
设置闹钟时alarmMgr.set(AlarmManager.RTC_WAKEUP,date.getTime(), alarmIntent);
我认为你date.getTIme()正在检索当前时间。因此,当尝试执行警报时,触发的时间已经过去。更好的是,你可以做一些延迟(比方说,五秒钟)来触发你的闹钟。只需使用该行 -
alarmMgr.set(AlarmManager.RTC_WAKEUP,date.getTime() + 5*1000, alarmIntent);