0

開始我是android開發初學者,這裏是我的情況:的TouchWiz停止和重新啓動時,我的前景在服務啓動

我實現它可以啓動的前臺服務,從我的應用程序/停止。

一切正常,直到我測試是在我的設備上,每次服務在引導完成時重新啓動,TouchWiz失敗並重新啓動。

編輯:現在重新啓動設備時,該服務儘管是不是運行在所有的事實開始(可能是我有存放一些布爾變量來承擔接收啓動完成了充分的行動)

我錯過了一些重要的東西,但無法弄清楚。 任何想法,可能是我缺少一些額外的權限,或者至少我怎麼能從Android設備日誌中獲得TouchWiz失敗時發生的情況。

接收器代碼:

public class MyServiceReceiver extends BroadcastReceiver { 
//Ctor: 
public MyServiceReceiver() {super();} 
//Receive Boot Action: 
@Override 
public void onReceive(Context context, Intent intent) { 
     if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) { 
      Intent restartServiceIntent = new Intent(context,MyService.class); 
      context.startService(restartServiceIntent); 
     } 
}} 

爲MyService

public class MyService extends Service { 
//ctor: 
public MyService() {} 
//On Create Service: 
@Override 
public void onCreate() { 
    super.onCreate(); 
    pNotification = setNotification(); 
    isRunning = false; 
    this.startForeground(SERVICE_NOTIFICATION_ID,pNotification); 
} 

//Notifications: 
public static final String SERVICE_NOTIFICATION_TAG = "My_SERVICE"; 
public static final int SERVICE_NOTIFICATION_ID = 22101982; 
private Notification pNotification; 
private Notification setNotification(){ 
    NotificationCompat.Builder notifBuilder = new NotificationCompat.Builder(MyApplication.getMyAppContext()); 
    notifBuilder.setAutoCancel(false); 
    notifBuilder.setContentTitle(getResources().getString(R.string.service_notification_title)); 
    notifBuilder.setContentText(getResources().getString(R.string.service_notification_message)); 
    notifBuilder.setSmallIcon(R.drawable.ic_action_record); 
    Intent mainIntent = new Intent(this,MainActivity.class); 
    PendingIntent pendingIntent = PendingIntent.getActivity(this,SERVICE_NOTIFICATION_ID,mainIntent,0); 
    notifBuilder.setContentIntent(pendingIntent); 
    return notifBuilder.build(); 
} 

public static boolean IsRunning(){return isRunning;} 
private static boolean isRunning; 
@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    //TODO:Initialisations 
    isRunning = true; 
    return START_STICKY; 
} 

//On Destroy Service: 
@Override 
public void onDestroy() { 
    super.onDestroy(); 
    isRunning = false; 
} 

@Override 
public IBinder onBind(Intent intent) {return null;}}//My service 

更新 只出現在 「重啓」,而不是當我關機的問題 - 啓動裝置。

+0

是否有任何logcat錯誤?你可以發佈你的代碼嗎? –

+0

我正在測試真實的設備上,使用android studio,我現在正在編輯我的問題 – Anas

+1

如果仍有錯誤,您將有logcat錯誤消息。請檢查它 –

回答

0

所以;問題只是消失了,唯一改變的是通知ID和PendingIntent ID,它們是一樣的..我不知道爲什麼這個問題在第一個地方提出,我繼續前進,但這使我擔心,我對它的起源感到好奇。

我認爲它來自通知服務顯示正在與touchwiz啓動序列或類似的事情。

反正我堅持這個問題,直到有人有一天會遇到同樣的好奇情況。

感謝您的建議