我的问题出现在android Marshmallow上,如果apk文件是调试那么一切正常,但是在发布模式下无法启动服务,不会崩溃只是进入睡眠状态。代码很简单
Intent intent = new Intent(MyActivity.this, WorkService.class);
startService(intent);
,也尝试了
Thread t = new Thread(){
public void run(){
if (null == WorkService.workThread) {
Intent intent = new Intent(MyActivity.this, WorkService.class);
startService(intent);
}
}
};
t.start();
结果是一样的,WorkService:
public class WorkService extends Service {
public static WorkThread workThread = null;
private static Handler mHandler = null;
private static List<Handler> targetsHandler = new ArrayList<>(5);
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
mHandler = new MHandler(this);
workThread = new WorkThread(mHandler);
workThread.start();
Log.v("DrawerService", "onCreate");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.v("DrawerService", "onStartCommand");
Message msg = Message.obtain();
msg.what = Global.MSG_ALLTHREAD_READY;
notifyHandlers(msg);
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
workThread.disconnectBt();
workThread.disconnectNet();
workThread.disconnectUsb();
workThread.quit();
workThread = null;
Log.v("DrawerService", "onDestroy");
}
static class MHandler extends Handler {
WeakReference<WorkService> mService;
MHandler(WorkService service) {
mService = new WeakReference<>(service);
}
@Override
public void handleMessage(Message msg) {
notifyHandlers(msg);
}
}
/**
*
* @param handler
*/
public static void addHandler(Handler handler) {
if (!targetsHandler.contains(handler)) {
targetsHandler.add(handler);
}
}
/**
*
* @param handler
*/
public static void delHandler(Handler handler) {
if (targetsHandler.contains(handler)) {
targetsHandler.remove(handler);
}
}
/**
*
* @param msg
*/
public static void notifyHandlers(Message msg) {
for (int i = 0; i < targetsHandler.size(); i++) {
Message message = Message.obtain(msg);
targetsHandler.get(i).sendMessage(message);
}
}
}
有人能帮助我吗?