Android殺せないプロセス

24651 ワード

最近、同僚と微信の殺害プロセスが終わった後、友达から微信という機能も受け取ることができます.以前はずっと使っていたが、この技術を研究しなかった.最近プロジェクトは早めに完成して、少し暇な时間があって、この技術を研究します.そしてdemoと書いて実行してみたらOKでした.ここで友达に分かち合うと、足りないところがあるかもしれませんが、大神たちの指摘を歓迎します.
 
まず、正常にプログラムを終了したり、手動で殺したりすると、プログラムは停止します.あるいはバックグラウンドで実行している間に360加速球を使って掃除をしないと、私たちのプロセスを殺すこともあります.個人理解360が加速すると、信頼を追加されないプロセスをforループでループし、ループの中で一つ一つkillします.そこで、私たちの応用では2つのプロセスを開き、1つのプロセスが殺されたとき、別のプロセスが殺されたプロセスを起動し、互いに守り合うという考え方がありました.この考えに沿って、コードを引っ張り始めた.
 
1.    Service:LocationService RemoteServices
LocationServiceRemoteServices      。     RemoteServices         Androidmanifest
<service
    android:name=".RemoteServices"
    android:enabled="true"
    android:exported="true"
    android:process=".RemoteServices" />
         service    ,         。              aidl,    aidl  ,           。
    ProcessIdle.aidl  
 
  
// ProcessIdle.aidl

package inner;
interface ProcessIdle {

     String getProcessName();

}
 
  
         getProcessName(),       。     LocationService RemoteServices   。
RemoteServices
public class RemoteServices extends Service {
    MyBinder myBinder;
    MyConn myConn;

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

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        showWindow("RemoteServices======");
        Toast.makeText(this, "RemoteServices=======onStartCommand", Toast.LENGTH_SHORT).show();
        boolean isLocationServiceRunning = Util.isServiceRunning(this, "com.example.zonglijia.killapp.LocationService");

        if (!isLocationServiceRunning) {
            RemoteServices.this.startService(new Intent(RemoteServices.this, LocationService.class));
        }
        RemoteServices.this.bindService(new Intent(RemoteServices.this, LocationService.class), myConn, Context.BIND_IMPORTANT);
        return START_STICKY;
    }

    @Override
    public void onCreate() {
        Toast.makeText(this, "RemoteServices=======onCreate", Toast.LENGTH_SHORT).show();
        Log.i("RemoteServices", "onCreate");
        super.onCreate();
        if (myBinder == null)
            myBinder = new MyBinder();
        if (myConn == null) {
            myConn = new MyConn();
        }

//        showWindow("RemoteServices======");
    }

    class MyBinder extends ProcessIdle.Stub {

        @Override
        public String getProcessName() throws RemoteException {
            return "RemoteServices";
        }
    }

    class MyConn implements ServiceConnection {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Toast.makeText(RemoteServices.this, "       ", Toast.LENGTH_LONG).show();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Toast.makeText(RemoteServices.this, "        ", Toast.LENGTH_LONG).show();

            RemoteServices.this.startService(new Intent(RemoteServices.this, LocationService.class));
            RemoteServices.this.bindService(new Intent(RemoteServices.this, LocationService.class), myConn, Context.BIND_IMPORTANT);

        }
    }

    WindowManager mWM;
    TextView view;

    private void showWindow(String text) {
        mWM = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
        WindowManager.LayoutParams params = new WindowManager.LayoutParams();
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        params.width = WindowManager.LayoutParams.WRAP_CONTENT;
        params.format = PixelFormat.TRANSLUCENT;
        params.type = WindowManager.LayoutParams.TYPE_TOAST;
        params.setTitle("Toast");
        params.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;

//        view = LayoutInflater.from(this).inflate(R.layout.window_show, null);
//        TextView textView = (TextView) view.findViewById(R.id.text);
        view = new TextView(this);
        view.setText(text);
        view.setTextColor(Color.RED);
        mWM.addView(view, params);
    }

    @Override
    public void onDestroy() {
        Toast.makeText(this, "RemoteServices=======onDestroy", Toast.LENGTH_SHORT).show();
        Log.i("RemoteServices", "onDestroy");
        RemoteServices.this.startService(new Intent(RemoteServices.this, LocationService.class));
        RemoteServices.this.bindService(new Intent(RemoteServices.this, LocationService.class), myConn, Context.BIND_IMPORTANT);
        super.onDestroy();
    }
}
 
 
LocationService

public class LocationService extends Service {

    MyBinder myBinder;
    MyConn2 myConn2;

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

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        showWindow("LocationServices======");
        Toast.makeText(this, "LocationService=======onStartCommand", Toast.LENGTH_SHORT).show();
        boolean isRemoteServiceRunning = Util.isServiceRunning(this, "com.example.zonglijia.killapp.RemoteServices");

        if (!isRemoteServiceRunning) {
            LocationService.this.startService(new Intent(LocationService.this, RemoteServices.class));
        }
        LocationService.this.bindService(new Intent(LocationService.this, RemoteServices.class), myConn2, Context.BIND_IMPORTANT);
        return START_STICKY;
    }

    @Override
    public void onCreate() {
        Toast.makeText(this, "LocationService=======onCreate", Toast.LENGTH_SHORT).show();
        Log.i("LocationService", "onCreate");
        super.onCreate();
        if (myBinder == null)
            myBinder = new MyBinder();
        if (myConn2 == null) {
            myConn2 = new MyConn2();
        }


//        showWindow("LocationServices======");
    }

    class MyBinder extends ProcessIdle.Stub {

        @Override
        public String getProcessName() throws RemoteException {
            return "LocationProcess";
        }
    }

    class MyConn2 implements ServiceConnection {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Toast.makeText(LocationService.this, "       ", Toast.LENGTH_LONG).show();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Toast.makeText(LocationService.this, "        ", Toast.LENGTH_LONG).show();
            LocationService.this.startService(new Intent(LocationService.this, RemoteServices.class));
            LocationService.this.bindService(new Intent(LocationService.this, RemoteServices.class), myConn2, Context.BIND_IMPORTANT);
        }
    }

    WindowManager mWM;
    TextView view;

    private void showWindow(String text) {
        mWM = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
        WindowManager.LayoutParams params = new WindowManager.LayoutParams();
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        params.width = WindowManager.LayoutParams.WRAP_CONTENT;
        params.format = PixelFormat.TRANSLUCENT;
        params.type = WindowManager.LayoutParams.TYPE_TOAST;
        params.setTitle("Toast");
        params.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;

//        view = LayoutInflater.from(this).inflate(R.layout.window_show, null);
//        TextView textView = (TextView) view.findViewById(R.id.text);
        view = new TextView(this);
        view.setText(text);
        view.setTextColor(Color.RED);
        mWM.addView(view, params);
    }

    @Override
    public void onDestroy() {
        Log.i("LocationService", "onDestroy");
        Toast.makeText(this, "LocationService=======onDestroy", Toast.LENGTH_SHORT).show();
        LocationService.this.startService(new Intent(LocationService.this, RemoteServices.class));
        LocationService.this.bindService(new Intent(LocationService.this, RemoteServices.class), myConn2, Context.BIND_IMPORTANT);
        super.onDestroy();
    }
}
 
             ,                  ,       ,       。           ,        。        
 
  
class MyConn2 implements ServiceConnection {
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {

        Toast.makeText(LocationService.this, "       ", Toast.LENGTH_LONG).show();

    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        Toast.makeText(LocationService.this, "        ", Toast.LENGTH_LONG).show();
        LocationService.this.startService(new Intent(LocationService.this, RemoteServices.class));
        LocationService.this.bindService(new Intent(LocationService.this, RemoteServices.class), myConn2, Context.BIND_IMPORTANT);

    }

}
 
  ,         ,           ,            ,        。
 
     , 5.0          ,5.0              ,    ,           。   5.0       ,