我试图在应用程序启动10分钟后生成警报。根据我的要求,这个警报应该工作,无论用户关闭应用程序,关闭并再次打开,甚至重启后。下面是我的代码。
MainActivity.java
import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.context=this;
startService(new Intent(getBaseContext(), MyService.class));
}
public void startService(View view) {
// startService(new Intent(getBaseContext(), MyService.class));
}
// Method to stop the service
public void stopService(View view) {
// stopService(new Intent(getBaseContext(), MyService.class));
}
}
MyService.java -这是服务类
import android.app.AlarmManager;
import android.app.IntentService;
import android.app.PendingIntent;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.IBinder;
import android.os.SystemClock;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Calendar;
import java.util.GregorianCalendar;
/**
* Created by Nirodha Mihiran on 6/8/2016.
*/
public class MyService extends Service {
public static boolean doneCreted = false;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Let it continue running until it is stopped.
Toast.makeText(this, "Service Started Second", Toast.LENGTH_LONG).show();
Calendar calendar = new GregorianCalendar();
calendar.setTimeInMillis(System.currentTimeMillis() + 10 * 60 * 1000);
intent = new Intent(MyService.this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MyService.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) MyService.this.getSystemService(MyService.this.ALARM_SERVICE);
// am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60 * 5, pendingIntent);
// am.setWindow(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(), 1000 * 60 * 5 , pendingIntent);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
}
}
AlarmReciever.java
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.widget.Toast;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.GregorianCalendar;
/**
* Created by thisararanawaka on 6/2/16.
*/
public class AlarmReceiver extends BroadcastReceiver {
private int MID = 0;
public static boolean isCreated;
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
long when = System.currentTimeMillis();
Calendar calendar = new GregorianCalendar();
calendar.setTimeInMillis(System.currentTimeMillis()+5*60*1000);
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
context).setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Alaram Fired")
.setContentText("Events To be Performed").setSound(alarmSound)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
notificationManager.notify(MID, mNotifyBuilder.build());
MID++;
Toast.makeText(context, "Notification Created .", Toast.LENGTH_LONG).show();
}
}
如果我没有关机和关机或重新启动手机,我在获得闹钟准时没有问题。我准时把闹钟响了。
然而,如果我关闭并打开手机,或者如果我重新启动手机,我就会在手机打开/重新启动后收到警报和通知-可能在一分钟后。在这种情况下,即使闹钟安排在明天也没关系,我一开机/重启手机就会收到。
我做错了什么?
通过查看您的代码,这可能是
在你的onStartCommand()中,你将时间设置为calendar.setTimeInMillis(System.currentTimeMillis() + 10 * 60 * 1000);
将日历时间设置为+ 10分钟,然后在下一行设置相同的时间am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
试试这样,
long myAlarmTime = System.currentTimeMillis() + 10 * 60 * 1000;
am.set(AlarmManager.RTC_WAKEUP, myAlarmTime, pendingIntent);
重新启动时遇到此问题的原因
服务可以被系统运行有两个原因。如果有人调用Context.startService(),那么系统将检索服务(创建它并在需要时调用它的onCreate()方法),然后使用客户端提供的参数调用它的onStartCommand(Intent, int, int)方法。此时,服务将继续运行,直到Context.stopService()或stopSelf()被调用。https://developer.android.com/reference/android/app/Service.html