Restart service after force stop

后端 未结 3 856
时光取名叫无心
时光取名叫无心 2020-12-06 12:48

I am developing an application which is used as application locker, an app which is able to protect other installed apps by asking the user for a password on opening those a

3条回答
  •  不知归路
    2020-12-06 13:04

    Create your own Exception Handler & whenever app crashes start service ..

    Your Exception Handler Class

    *public class MyHandler implements UncaughtExceptionHandler {
        private Context m_context;
    
    
        public static void attach(Context context) {
            Thread.setDefaultUncaughtExceptionHandler(
                new MyHandler(context)
            );
        }
    
        ///////////////////////////////////////////// implementation
    
        private MyHandler(Context context) {
            m_context=context;
        }
    
        public void uncaughtException(Thread thread,Throwable exception) {
        /*  StringWriter stackTrace=new StringWriter();
            exception.printStackTrace(new PrintWriter(stackTrace));*/
            System.out.println("ERROR IS "+(exception));
    
    
            Intent intent=new Intent("com.sample.service.serviceClass");  
                m_context.startService(intent);
    
            // from RuntimeInit.crash()
            Process.killProcess(Process.myPid());
            System.exit(10);
        }
    }*
    

    Attach In Your Activity

    public class MyActivity extends Activity{
    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.grid_layout);
            MyHandler.attach(this);
    }
    }
    

提交回复
热议问题