在销毁app之后在自定义服务中运行自定义线程

时间:2016-12-28 07:28:50

标签: android service timer background thread-safety

我好几天,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

1 个答案:

答案 0 :(得分:0)

阅读此SO Thread
如果您不想让它被OS杀死,您需要获得服务通知 您也可以使用计时器来运行/检查您的服务是否正在运行。您可以阅读此SO Thread来实现此方法