2016-12-28 19 views
0

我幾天後,與Android服務的問題。 我從服務寫了自定義類 我從線程寫了自定義類。在銷燬應用程序後運行自定義線程中的自定義服務

我希望我的服務在銷燬程序後發送通知,一切正常,但定製類不工作後,但在停止或暫停應用程序後,我的服務工作正確。
請幫我解決這個問題。

ServiceNotification.class

import Threadcustom; 

public class ServiceNotification extends Service { 

    @Override 
    public void onCreate() { 
    super.onCreate(); 
    } 

    @Override 
    public int onStartCommand(final Intent intent, int flags, int startId) { 
    GlobalController.timerTaskService = new TimerTask() { 
     @Override 
     public void run() { 
     Threadcustom threadc = new Threadcustom(); 
      threadc.setDetail("1"); 
      threadc.start(); 
     Threadcustom threadc1 = new Threadcustom(); 
      threadc1.setDetail("2"); 
      threadc1.start(); 
     if (threadc.newMessage == 1) { 
      CustomNotification customNotification = new CustomNotification(); 
      customNotification.notifiaction("my message"); 
     } 
     } 
    }; 
    GlobalController.timerService.schedule(GlobalController.timerTaskService, 0, 5000); 
    return Service.START_STICKY; 
    } 

    public void onTaskRemoved(Intent rootIntent) { 
    Intent restartService = new Intent(getApplicationContext(), 
     this.getClass()); 
    restartService.setPackage(getPackageName()); 
    PendingIntent restartServicePI = PendingIntent.getService(
     getApplicationContext(), 1, restartService, 
     PendingIntent.FLAG_ONE_SHOT); 
    AlarmManager alarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE); 
    alarmService.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + 5000, restartServicePI); 

    } 

    @Override 
    public void onDestroy() { 
    super.onDestroy(); 
    } 

    @Override 
    public IBinder onBind(Intent arg0) { 
    return null; 
    } 


    @Override 
    public void onRebind(Intent intent) { 
    super.onRebind(intent); 
    } 

} 

的manifest.xml

<receiver 
    android:name=".services.ServiceBootCompleteReciver" 
    android:enabled="true" > 
    <intent-filter> 
     <action android:name="android.intent.action.BOOT_COMPLETED"/> 
     <category android:name="android.intent.category.DEFAULT"/> 
    </intent-filter> 
</receiver> 
<service 
    android:name=".services.ServiceNotification" 
    android:enabled="true" 
    android:exported="false" 
    /> 

Threadcustom.class

public class ThreadSms extends Thread { 
@Override 
    public void run() { 
    super.run(); 
     define my url 
define http url connection get data from url then insert data to sqlite database in my application. 

    } 

我的工作線程糾正我的服務工作,線程和發送通知,但破壞之後應用程序通知工作,但我的自定義線程沒有調用,任何程序不只是崩潰所述警告

ClassLoader referenced unknown path: /data/app/app.myapp/lib/arm 
ClassLoader referenced unknown path: /system/framework/tcmclient.jar 

回答

0

閱讀本SO Thread
您需要爲您服務的通知,如果你不希望它被OS
打死你也可以用計時器運行/檢查你的服務運行還是不行。你可以閱讀這個SO Thread執行此方法

+0

tnanks男人,我的問題消失了。 – user3750973

+0

@ user3750973您的歡迎夥伴 –

+0

當屏幕鎖定或睡眠時,它可以工作嗎?我可以在屏幕鎖定或睡眠電話上工作嗎? – user3750973

相關問題