2016-11-25 34 views
0

我只是使用的服務,使像chathead,這是我的結果Android上運行的類

enter image description here

,但只有我有兩個問題

第一段時間我的服務被殺我注意到這是在弱設備,所以我怎麼能防止它在同一時間殺死Facebook的使者從未殺死

秒我的課動畫不順利我想我必須在新線程上運行該課

xxx = new classanimation (mContext);

我想這

new Handler(Looper.getMainLooper()).post(new Runnable() { 
     @Override 
     public void run() { 
      xxx = new classanimation (mContext); 

     } 
    }); 

,但我看到了同樣的沒有什麼不同

,這是我的服務代碼

public class MyCustomService extends Service { 


private volatile HandlerThread mHandlerThread; 
private ServiceHandler mServiceHandler; 
private static Context mContext; 
Handler mHandler; 
IBinder mBinder = new LocalBinder(); 
public static Socket client; 
public classanimation xxx; 




@Override 
public IBinder onBind(Intent intent) { 
    return mBinder; 
} 

public class LocalBinder extends Binder { 
    public MyCustomService getServerInstance() { 
     return MyCustomService.this; 
    } 
} 

private final class ServiceHandler extends Handler { 
    public ServiceHandler(Looper looper) { 
     super(looper); 
    } 

    // Define how to handle any incoming messages here 
    @Override 
    public void handleMessage(Message message) { 
     // ... 
     // When needed, stop the service with 
     // stopSelf(); 
    } 
} 


public void onCreate() { 
    super.onCreate(); 
    this.mContext = this; 

    xxx= new classanimation(mContext); //class i run it but its not smooth 

    mHandler = new Handler(Looper.getMainLooper()); 


    // An Android handler thread internally operates on a looper. 
    mHandlerThread = new HandlerThread("MyCustomService.HandlerThread"); 

    mHandlerThread.start(); 
    // An Android service handler is a handler running on a specific background thread. 
    mServiceHandler = new ServiceHandler(mHandlerThread.getLooper()); 

} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    // Send empty message to background thread 
    mServiceHandler.sendEmptyMessageDelayed(0, 500); 
    // or run code in background 
    mServiceHandler.post(new Runnable() { 

     @Override 
     public void run() { 
      // Do something here in background! 
      SessionManager session = new SessionManager(mContext); 



      // If desired, stop the service 
      //stopSelf(); 
     } 
    }); 
    // Keep service around "sticky" 
    return START_STICKY; 
} 

@Override 
public void onDestroy() { 
    // Cleanup service before destruction 
    mHandlerThread.quit(); 

} 

} 

回答

0

爲了確保您的服務是永遠遇難,你可以通過使用notifiaction使你的服務成爲前臺服務。這意味着服務運行時總會有一個通知圖標,但它永遠不會被殺死。例如:

Notification notification = new Notification(R.drawable.icon, getText(R.string.ticker_text), System.currentTimeMillis()); 
Intent notificationIntent = new Intent(this, ExampleActivity.class); 
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 
notification.setLatestEventInfo(this, getText(R.string.notification_title), getText(R.string.notification_message), pendingIntent); 
startForeground(ONGOING_NOTIFICATION_ID, notification); 
+0

感謝回覆我會盡力的:)仍然順利動畫的問題 –

0

不能殺死預防,是的,但你可以儘量減少在不同的工藝比你的應用程序啓動服務殺的概率。

+0

你我同意ak0692。你正在使用主要的Looper,你應該有不同的過程。示例代碼。 HandlerThread thread = new HandlerThread(「MyCustomService」,Process.THREAD_PRIORITY_BACKGROUND); thread.start(); 現在使用此thread.getLooper()作爲處理程序。 –

+0

謝謝我會試試這個:)還是動畫類不順暢的問題 –

相關問題