java - AlarmManager启动的IntentService不起作用

标签 java android service alarmmanager intentservice


我试图通过 AlarmManager 启动 IntentService,但该服务未启动。 (显然我的服务是在 list 中定义的......)
这是一些代码:

启动闹钟服务:

    Intent myIntent = new Intent(Main.this, TestsNotification.class);
    PendingIntent pendingIntent = PendingIntent.getService(Main.this, 0, myIntent, 0);
             AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
             Calendar calendar = Calendar.getInstance();
             calendar.setTimeInMillis(System.currentTimeMillis());
             calendar.set(Calendar.HOUR_OF_DAY, 15);
             alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

这是我的 IntentService:

public class TestsNotification extends IntentService {
    public TestsNotification() {
        super("myApp");
    }
    private SharedPreferences settings;
    private final String PREFERENCE_SETTINGS_FILENAME = "Settings";
    private int number=0;
    Tests tests;
@Override
public void onCreate() {
 // TODO Auto-generated method stub
    settings = getSharedPreferences(PREFERENCE_SETTINGS_FILENAME, MODE_PRIVATE);
    tests=new Tests();
}

@Override
public IBinder onBind(Intent intent) {
 // TODO Auto-generated method stub
 return null;
}

@Override
public void onDestroy() {
 // TODO Auto-generated method stub
 super.onDestroy();
}

@Override
public void onStart(Intent intent, int startId) {
 // TODO Auto-generated method stub
 super.onStart(intent, startId);
}

@Override
public boolean onUnbind(Intent intent) {
 // TODO Auto-generated method stub
 return super.onUnbind(intent);
}
public void Notify(String title)
{
    NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    Intent intent= new Intent (this,Splash.class);
    PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
    String body = " בליך";
    //String title = "יש מחר מבחן!";
    Notification n =new Notification(R.drawable.test, body, System.currentTimeMillis());
    n.flags |=Notification.FLAG_AUTO_CANCEL;
    n.setLatestEventInfo(getApplicationContext(), title, body, pi);
    n.defaults = Notification.DEFAULT_ALL;
    number++;
    n.number=number;
    try {
    nm.notify(0,n);
    FileMethods FM = new FileMethods(this);
    Date current = new Date();
    FM.Write("LOG", "I Dont Care!",FM.Read("LOG", this, "") + current.getDay()+"/"+current.getMonth()+"/"+ current.getYear()+" "+current.getHours()+":"+current.getMinutes() + "Notified" + title+ "\n");
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}
@Override
protected void onHandleIntent(Intent intent) {
    // TODO Auto-generated method stub
    String Attribute = "Class";
    String info = settings.getString(Attribute, "none");
    if(!info.equals("none")) {
        String classLetter = info.substring(0, info.lastIndexOf(" "));
        String classNum1 = info.substring(info.lastIndexOf(" ")+1);
        int classNum = Integer.parseInt(classNum1);
        try {
            Tests nextTest = this.tests.GetTests(classLetter, classNum)[0];
            Date current = new Date();
            Date testDate = new GregorianCalendar(nextTest.getDate().getYear(), nextTest.getDate().getMonth(), Integer.parseInt(nextTest.getDate().getDay())).getTime();
            long difference = testDate.getTime()-current.getTime();
            if (difference <=86400000) {
                Notify("יש מחר מבחן!");
            }//SHOULDNT BE HARDCODED!!!
            else {
                Notify("אין מחר מבחן!!");
            }
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
}

FileMethods 只是一个处理文件读写的类(以确保 AlarmManager 每 1 天运行一次)

谢谢!!

最佳答案

由于您使用的是 set(),因此您现在指定的时间有 40% 的可能性是过去的时间。

此外,如果设备当时处于 sleep 状态,它可能会在您的服务启动之前重新进入休眠状态。成功使用 _WAKEUP 警报有一个非常具体的模式,涉及 BroadcastReceiverWakeLock。我的WakefulIntentService尝试为您处理一些此类工作。

此外,onStart() 已被弃用相当多的时间,不应在 IntentService 上实现。

关于java - AlarmManager启动的IntentService不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9545301/

相关文章:

java - 除非我在本地主机中,否则无法访问 Tomcat Manager

模拟器的 android 8.0 api 26 arm64-v8a 系统镜像

android - 使用 Google Cardboard 改进 Android 上的头部追踪

android - 保持 ListView 选择颜色

android - 正确的工作流程以从 Internet 检索数据并显示在 ListView 中

java - 即使应用程序未运行,如何每 10 分钟在后台获取位置信息?

java - 构建安卓应用 :I can sending data to database in localhost but not the same in online database

java - jquery是否可以同时提交表单并发送ajax请求

algorithm - SOA 是否支持方法组合?

java - JRebel 曾经在生产环境中使用过吗?它可以在 JVM 上重新加载什么?