只有當用戶按下刷新按鈕時,我的服務纔會在系統啓動時啓動。我試過在堆棧中搜索everythng,但沒有我重新編寫我的代碼4次,沒有什麼,我希望我的服務在一段時間內運行,當按下刷新按鈕時,我不明白爲什麼不在啓動時運行,我錯過了什麼?啓動時的Android啓動服務,無法接收廣播
的Manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.moody"
android:installLocation="internalOnly"
android:versionCode="0"
android:versionName="0.6.7.2 alpha" >
<permission
android:name="com.android.moody.permission.GET_SERVER_DATA"
android:protectionLevel="normal" />
<uses-sdk
android:maxSdkVersion="18"
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<uses-permission android:name="com.android.moody.permission.GET_SERVER_DATA" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:allowClearUserData="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="activities.MainActivity"
android:label="@string/app_name" >
</activity>
<activity
android:name="activities.Menu_esq"
android:label="@string/title_activity_menu_esq" >
</activity>
<activity
android:name="activities.BaseActivity"
android:label="@string/title_activity_base" >
</activity>
<activity
android:name="activities.MainView"
android:label="@string/title_activity_main_view" >
</activity>
<activity
android:name="activities.LoginActivity"
android:label="@string/app_name"
android:noHistory="true"
android:windowSoftInputMode="adjustResize|stateVisible" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.moody.LeftActivity"
android:label="@string/title_activity_left" >
</activity>
<activity
android:name="com.example.moody.RightActivity"
android:label="@string/title_activity_right" >
</activity>
<activity
android:name="activities.UserDetailsActivity"
android:label="@string/title_activity_user_details" >
</activity>
<activity
android:name="fragments.FragTopicsPreview"
android:label="@string/title_activity_copy_of_topics_preview" >
</activity>
<activity android:name="activities.LoadingActivity" >
</activity>
<service
android:name="service.ServiceBackground"
android:enabled="true"
android:icon="@drawable/ic_launcher"
android:label="@string/moody_service" >
</service>
<receiver android:name="service.Alarm" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<receiver
android:name="service.StartServiceReceiver"
android:permission="com.android.moody.permission.GET_SERVER_DATA" >
<intent-filter>
<action android:name="moody_get_data" />
</intent-filter>
</receiver>
</application>
ServiceBackground
public class ServiceBackground extends Service {
Alarm alarm = new Alarm();
public ServiceBackground() {
// TODO Auto-generated constructor stub
}
private boolean isRunning = false;
Object getContent;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
super.onCreate();
// android.os.Debug.waitForDebugger();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
// Starts the alarm
alarm.setAlarm(getApplicationContext());
// Announcement about starting
Log.d("service", "Service Started");
// Start a Background thread
isRunning = true;
Thread backgroundThread = new Thread(new BackgroundThread());
backgroundThread.start();
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
// Stop the Background thread
isRunning = false;
}
private class BackgroundThread implements Runnable {
public void run() {
try {
while (isRunning) {
Log.d("service", "Thread started");
new ManContents().getAll(getResources(),
getApplicationContext());
isRunning = false;
}
stopSelf();
} catch (Exception e) {
e.printStackTrace();
}
}
}
報警
public class Alarm extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
context.startService(new Intent(context, ServiceBackground.class));
}
public void setAlarm(Context context) {
String alarm = Context.ALARM_SERVICE;
AlarmManager am = (AlarmManager) context.getSystemService(alarm);
Intent intent = new Intent("moody_get_data");
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
int type = AlarmManager.ELAPSED_REALTIME_WAKEUP;
// long interval = AlarmManager.INTERVAL_FIFTEEN_MINUTES;
long interval = 1000 * 60 * 2;
long triggerTime = SystemClock.elapsedRealtime() + interval;
am.setRepeating(type, triggerTime, interval, pi);
}
public void CancelAlarm(Context context) {
Intent intent = new Intent(context, Alarm.class);
PendingIntent sender = PendingIntent
.getBroadcast(context, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(sender);
}
}
StartServiceReceiver
public class StartServiceReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("service", "Starting service from serviceReceiver");
Intent service = new Intent(context, ServiceBackground.class);
context.startService(service);
}
}
注意:
- 我已經嘗試過的情況下直接過濾器啓動該服務,但對於 沒有理由不要開始,所以使用過濾器是 工作的唯一方式。
- 我也試過沒有StartServiceReceiver.java,意圖在報警器內但沒有運氣,如果有人認爲它的問題,我可以再次更改代碼。
- 我在線程中的while(isRunning)只是爲了將來的目的,但我認爲它不是因爲這個問題。
您使用的是Htc設備嗎?如果是這樣,請檢查:http://stackoverflow.com/questions/9525907/htcs-fast-boot-is-not-broadcasting-boot-completed-intent-nor-wiping-intents-f/18467723#18467723 – Lovis
nop,我使用genyMotion模擬器和nexus 4 – firetrap