由于stackoverlow用户的礼貌,我们编写了这段代码。应该在指定的小时开始敬酒,但不会跑。
有人可以帮我发现问题所在吗? 谢谢!
public class UnUsedService extends Service {
private PendingIntent pendingIntent;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "Service Created", Toast.LENGTH_LONG).show();
//startService(new Intent(this, UnUsedService.class));
}
@Override
public void onDestroy() {
super.onDestroy();
//Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
}
@Override
public void onStart(Intent intent, int startId) {
// super.onStart();
super.onStart(intent, startId);
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
//Toast.makeText(UnUsedService.this, "Start Alarm", Toast.LENGTH_LONG).show();
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 22);
calendar.set(Calendar.MINUTE, 30);
calendar.set(Calendar.SECOND, 0); AlarmManager am = (AlarmManager) getApplicationContext().getSystemService (Context.ALARM_SERVICE);
PendingIntent pi = PendingIntent.getService(getApplicationContext(), 0, new Intent(getApplicationContext(), AlarmReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi);
}};
接收器类
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Alarm worked.", Toast.LENGTH_LONG).show();
}
}
清单:
<service android:name="UnUsedService">
<intent-filter>
<action
android:name="org.gortcloud.startatboot.UnUsedService" />
</intent-filter>
</service>
<receiver android:name=".AlarmReceiver" android:process=":remote"/>
答案 0 :(得分:0)
我正在做类似的事情。您可以将receiver android:name=".AlarmReceiver"
替换为适用于我的receiver android:name=".AlarmReceiver" android:process=":remote"
。
答案 1 :(得分:0)
pendingIntent.setBroadcast(..)
需要一个BroadcastReceiver,现在我正在使用pendingIntent.setService(...)
我有一个扩展IntentService的类(带有noarg构造函数)。我成功接到警报以触发启动此服务。我有一个onStart()
和onStartCommand()
以及onHandleIntent()
每个都可以运行(我假设你只会实现其中一个)
希望这会对你有所帮助。