gpt4 book ai didi

android - Android中编写后台服务

转载 作者:搜寻专家 更新时间:2023-11-01 09:00:57 26 4
gpt4 key购买 nike

我的 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>

现在,当我启动应用程序时,我会调用一个 Activity 类,它基本上使用下面的代码调用服务...但每 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;
}

}

请指教,缺少什么?

最佳答案

Current Alarm Manager state:




Realtime wakeup (now=2013-02-27 20:32:52):



RTC_WAKEUP #1: Alarm{412e05f0 type 0 com.android.providers.calendar}
type=0 when=+20h57m39s289ms repeatInterval=0 count=0
operation=PendingIntent{412e05e0: PendingIntentRecord{412e04f0 com.androi
roviders.calendar broadcastIntent}}
RTC_WAKEUP #0: Alarm{41566480 type 0 android}
type=0 when=+10h56m12s700ms repeatInterval=3725515 count=0
operation=PendingIntent{4162dbc0: PendingIntentRecord{41591808 android br
castIntent}}
RTC #1: Alarm{41532120 type 1 android}
type=1 when=+22h27m7s419ms repeatInterval=0 count=0
operation=PendingIntent{4154e7c8: PendingIntentRecord{415adb68 android br
castIntent}}
RTC #0: Alarm{414eb330 type 1 com.android.calendar}
type=1 when=+3h27m7s419ms repeatInterval=0 count=0
operation=PendingIntent{414eb320: PendingIntentRecord{4166c020 com.androi
alendar broadcastIntent}}

Elapsed realtime wakeup (now=+1h5m9s472ms):
ELAPSED_WAKEUP #1: Alarm{415608e0 type 2 android}
type=2 when=+56m18s779ms repeatInterval=0 count=0
operation=PendingIntent{412fbbc0: PendingIntentRecord{412d26f0 android br
castIntent}}
ELAPSED_WAKEUP #0: Alarm{41b31c20 type 2 com.android.phone}
type=2 when=+21s200ms repeatInterval=0 count=0
operation=PendingIntent{41b31c10: PendingIntentRecord{4163fea8 com.androi
hone broadcastIntent}}
ELAPSED #3: Alarm{41b21d40 type 3 android}
type=3 when=+22h57m0s588ms repeatInterval=0 count=0
operation=PendingIntent{41567800: PendingIntentRecord{41b61370 android br
castIntent}}
ELAPSED #2: Alarm{41b792e8 type 3 android}
type=3 when=+9m50s528ms repeatInterval=1800000 count=1
operation=PendingIntent{415af0b0: PendingIntentRecord{41b791c0 android br
castIntent}}
ELAPSED #1: Alarm{415d6648 type 3 android}
type=3 when=+7m30s477ms repeatInterval=0 count=0
operation=PendingIntent{415652b0: PendingIntentRecord{41bba838 android br
castIntent}}
ELAPSED #0: Alarm{417b4448 type 3 android}
type=3 when=+7s294ms repeatInterval=0 count=0
operation=PendingIntent{4162f708: PendingIntentRecord{415c23a0 android br
castIntent}}

Broadcast ref count: 0

Alarm Stats:
android
20685ms running, 0 wakeups
65 alarms: act=android.intent.action.TIME_TICK flg=0x40000014
2 alarms: act=com.android.server.action.NETWORK_STATS_POLL flg=0x14
6 alarms: act=com.android.server.ThrottleManager.action.POLL flg=0x14
1 alarms: act=com.android.server.NetworkTimeUpdateService.action.POLL flg
14
com.android.providers.calendar
8935ms running, 1 wakeups
1 alarms: act=com.android.providers.calendar.intent.CalendarProvider2 flg
14
com.android.phone
2632ms running, 63 wakeups
63 alarms: act=com.android.internal.telephony.gprs-data-stall flg=0x14

关于android - Android中编写后台服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15010316/

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