我只是使用服务来制作像chathead这样的东西,这是我的结果
但只有我有两个问题
有一段时间,我的服务被杀,我注意到弱设备上有这样我怎样才能防止它在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 :(得分:1)
为了确保您的服务永远不会被杀死,您可以使用通知来使您的服务成为前台服务。这意味着服务运行时始终会有一个通知图标,但它永远不会被杀死。例如:
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);
答案 1 :(得分:0)
你无法防止被杀,是的,但你可以通过在不同的过程中启动服务来最小化杀人的可能性。