gpt4 book ai didi

Android 闹钟,服务未被调用

转载 作者:行者123 更新时间:2023-11-30 04:10:55 28 4
gpt4 key购买 nike

在我的 Activity 中:

Intent myIntent = new Intent(this, MyAlarmService.class);
pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);

AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 10);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

Toast.makeText(this, "Start Alarm", Toast.LENGTH_LONG).show();

要调用的类:

public class MyAlarmService extends Service {

@Override
public void onCreate() {
// TODO Auto-generated method stub
Toast.makeText(this, "MyAlarmService.onCreate()", Toast.LENGTH_LONG).show();
}

@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(this, "MyAlarmService.onBind()", Toast.LENGTH_LONG).show();
return null;
}

@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG).show();
}
}

使用此代码,MyAlarmService 未被调用。为什么?此代码已从示例中删除,因此经过测试可以正常运行。我会忘记什么吗?

最佳答案

我想,你的服务已经正常启动了。 Toast在服务中出现问题。

尝试将 Toast.makeText() 更改为 Log.d() 并查看记录是否出现在您的 Logcat 中。

服务中的 Toast 问题已得到解释 here .

关于Android 闹钟,服务未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10883377/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com