Java 如何在应用程序关闭的确切时间发送通知?
我是android studio的新手。我需要创建一个具有提醒功能的便笺应用程序。我决定通过alarmmanager执行此操作,但它仅在应用程序打开时才起作用。仅通过服务实现这一点是否现实? 我注意到在google keep应用程序中,当创建提醒时,会创建一个新的服务。如何做到这一点 活动中的部分代码:Java 如何在应用程序关闭的确切时间发送通知?,java,android,service,notifications,alarmmanager,Java,Android,Service,Notifications,Alarmmanager,我是android studio的新手。我需要创建一个具有提醒功能的便笺应用程序。我决定通过alarmmanager执行此操作,但它仅在应用程序打开时才起作用。仅通过服务实现这一点是否现实? 我注意到在google keep应用程序中,当创建提醒时,会创建一个新的服务。如何做到这一点 活动中的部分代码: public void alarm(Calendar c) { AlarmManager alarmManager = (AlarmManager) getSystemServi
public void alarm(Calendar c) {
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlertReceiver.class);
intent.putExtra("title",editTask.getText().toString());
PendingIntent pen = PendingIntent.getBroadcast(this, 1, intent, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(),pen);
AlertReceiver.java
package denchic45.myapp.appmy;
import android.content.BroadcastReceiver;
import android.content.Intent; import android.widget.Toast;
import android.content.Context;
public class AlertReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
intent = new Intent(context, Services.class);
context.startService(intent);
Toast.makeText(context, "alarm", Toast.LENGTH_SHORT).show();
} }
Services.java
package denchic45.myapp.appmy;
import android.app.IntentService;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
public class Services extends IntentService {
public Services() {
super("public services");
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
protected void onHandleIntent(@Nullable Intent intent) {
Context context = getApplicationContext();
Toast.makeText(context, "WORK", Toast.LENGTH_SHORT).show();
final String CHANNEL_ID = "1";
if(Build.VERSION.SDK_INT = Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(CHANNEL_ID,"001",
NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription("description");
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.createNotificationChannel(channel);
NotificationCompat.Builder notification = new NotificationCompat.Builder(context, CHANNEL_ID)
.setContentTitle("dd")
.setSmallIcon(R.mipmap.ic_launcher);
notification.build();
NotificationManagerCompat managerCompat = NotificationManagerCompat.from(context);
managerCompat.notify(1,notification.build());
} else {
NotificationCompat.Builder notification = new NotificationCompat.Builder(context, CHANNEL_ID)
.setContentTitle("dd")
.setSmallIcon(R.drawable.note)
.setChannelId(CHANNEL_ID);
notification.build();
NotificationManagerCompat managerCompat = NotificationManagerCompat.from(context);
managerCompat.notify(1,notification.build());
}
} }
清单中的部分代码:
<receiver android:name=".AlertReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name=".Services"/>
我会尽量降低代码的复杂性,缩小到相关的位。例如,您可以从广播接收器设置警报,如下所述:您好,谢谢您的回复。它能在7安卓系统上运行吗?应该可以,但它可能需要白名单来优化电池以保持永久运行。我会尽量降低代码的复杂性,缩小到相关的位。例如,您可以从广播接收器设置警报,如下所述:您好,谢谢您的回复。它能在7安卓系统上工作吗?应该可以,但它可能需要白名单来优化电池以保持永久运行。