0

在 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"/>
4

2 回答 2

0

好的...我得到了这个工作。我关注了如何在 Android 中使用警报管理器启动服务?. 基本上我使用的pendingIntent.setBroadcast(..)需要广播接收器的,现在我正在使用pendingIntent.setService(...)

我有一个扩展 IntentService 的类(带有 noarg 构造函数)。我已成功收到警报以触发启动此服务。我有一个onStart()andonStartCommand()并且onHandleIntent()每个都运行(我假设你只会实现其中一个)

希望这可以帮助你。

于 2012-12-14T15:42:09.610 回答
0

我只是在做类似的事情。你能用对我有用的代替receiver android:name=".AlarmReceiver"吗?receiver android:name=".AlarmReceiver" android:process=":remote"

于 2012-12-13T17:54:05.963 回答