我的Android应用程序有要求在某个时间安排任务。它可以是每日,每周,每月等。因此,当插入输入数据时,如果用户选择“每日”。将会有后勤服务,每天都会插入相同的数据。在Android中写入后台服务
我试图创建服务和相应的接收者类,但它看起来像服务不会自动运行。有人能告诉我什么是缺少的?
接收机类
public class MyScheduleReceiver extends BroadcastReceiver {
// Restart service every 30 seconds
private static final long REPEAT_TIME = 1000 * 30;
@Override
public void onReceive(Context context, Intent intent) {
AlarmManager service = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, MyStartServiceReceiver.class);
PendingIntent pending = PendingIntent.getBroadcast(context, 0, i,
PendingIntent.FLAG_CANCEL_CURRENT);
Calendar cal = Calendar.getInstance();
// Start 30 seconds after boot completed
cal.add(Calendar.SECOND, 30);
//
// Fetch every 30 seconds
// InexactRepeating allows Android to optimize the energy consumption
service.setInexactRepeating(AlarmManager.RTC_WAKEUP,
cal.getTimeInMillis(), REPEAT_TIME, pending);
// service.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
// REPEAT_TIME, pending);
Toast.makeText(context, "Receving", Toast.LENGTH_SHORT).show();
}
public class MyStartServiceReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, LocalWordService.class);
context.startService(service);
}
}
的Android Menifest.xml
<service
android:name=".LocalWordService"
android:label="LocalWordService" >
</service>
<receiver android:name="MyScheduleReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<receiver android:name="MyStartServiceReceiver" >
</receiver>
现在,当我启动应用程序,我叫一个活动类,它使用下面的代码基本上拨打服务......但什么也没有发生每30秒后。
Intent mServiceIntent = new Intent(this, LocalWordService.class);
startService(mServiceIntent);
服务类
public class LocalWordService extends Service {
private final IBinder mBinder = new MyBinder();
private ArrayList<String> list = new ArrayList<String>();
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "Starting", Toast.LENGTH_SHORT).show();
Random random = new Random();
if (random.nextBoolean()) {
list.add("Linux");
}
if (random.nextBoolean()) {
list.add("Android");
}
if (random.nextBoolean()) {
list.add("iPhone");
}
if (random.nextBoolean()) {
list.add("Windows7");
}
if (list.size() >= 20) {
list.remove(0);
}
return Service.START_NOT_STICKY;
}
@Override
public IBinder onBind(Intent arg0) {
return mBinder;
}
public class MyBinder extends Binder {
LocalWordService getService() {
return LocalWordService.this;
}
}
public List<String> getWordList() {
return list;
}
}
请指点,缺什么?
使用'亚行外壳dumpsys alarm'看到你报警的时间表是什么样子。另外,请检查LogCat以获取警告。 – CommonsWare 2013-02-21 19:13:36
是否执行此代码的活动? 'Intent mServiceIntent = new Intent(this,LocalWordService.class); startService(mServiceIntent);' 和你的服务类是在同一个包中? 你也可以提供logcat的堆栈跟踪吗? – 2013-02-21 19:19:42
确保您的服务在清单中声明。 – Matthew 2013-02-21 20:37:17