如何在Android中创建一个无法被Advanced Task Killer杀死的服务

时间:2011-07-27 16:33:29

标签: java android android-widget android-ndk android-manifest

我创建了一个安全应用程序,就像Mcafee wave secure一样 我的应用程序正在侦听SMS命令并在命令匹配时执行某些操作,因此我创建了一个表单,其中包含另一个用于侦听SMS的服务。

这是主要形式:

public static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
ArrayList<String> messageList;
ArrayAdapter< String> adapter;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //untuk mendisable notification area
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                            WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.main);
    MainButtonAbout=(Button) findViewById(R.id.MainbuttonAbout);
    MainButtonHelp=(Button) findViewById(R.id.MainbuttonHelp);
    MainButtonWizard=(Button) findViewById(R.id.MainbuttonWizard);
    MainButtonOption=(Button) findViewById(R.id.MainbuttonOption);
    MainCheckBoxActive=(CheckBox)findViewById(R.id.MaincheckBoxActive);

    MainButtonAbout.setOnClickListener(this);
    MainButtonHelp.setOnClickListener(this);
    MainButtonWizard.setOnClickListener(this);
    MainButtonOption.setOnClickListener(this);

    startService(new Intent(MainForm.this, ListenSMSservice.class));

    MainCheckBoxActive.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // TODO Auto-generated method stub
            if (buttonView.isChecked())
            {
                Toast.makeText(MainForm.this, "Your Device is Protected Now!!", 1).show();              
                startService(new Intent(MainForm.this, ListenSMSservice.class));
            }
            else
            {
                Toast.makeText(MainForm.this, "Your Device is not Protected Now!!", 1).show();
                stopService(new Intent(MainForm.this, ListenSMSservice.class));
            }
        }
    });

}

public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.MainbuttonAbout:
        Intent GoToAbout= new Intent(this,AboutForm.class);
        startActivity(GoToAbout);
        break;
    case R.id.MainbuttonHelp:
        Intent GoToHelp= new Intent(this,HelpForm.class);
        startActivity(GoToHelp);
        break;
    case R.id.MainbuttonWizard:
        Intent GoToWizard1= new Intent(this,WizardForm1.class);
        startActivity(GoToWizard1);
        break;
    case R.id.MainbuttonOption:
        Intent GoToOption= new Intent(this,OptionForm.class);
        startActivity(GoToOption);
        break;  
    default:
        break;
    }
}

这是服务表格:

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

@Override
public void onCreate() {
    Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();

}

/*@Override
public void onStart(Intent intent, int startid) {
    Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
    ListenSMS();
}*/

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    ListenSMS();
    // We want this service to continue running until it is explicitly
    // stopped, so return sticky.
    return START_STICKY;
}

private void ListenSMS() {
    // TODO Auto-generated method stub
    messageList  = new ArrayList<String>();
    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, messageList);

    //Toast.makeText(this, "Masuk bagian sini sudah", 1).show();

    IntentFilter filter = new IntentFilter(SMS_RECEIVED);
    registerReceiver(receiver_SMS, filter);
}

BroadcastReceiver receiver_SMS = new BroadcastReceiver()
{
    public void onReceive(Context context, Intent intent)
    {
         if (intent.getAction().equals(SMS_RECEIVED))
         {
                Bundle bundle = intent.getExtras();
                if (bundle != null)
                {
                  Object[] pdus = (Object[]) bundle.get("pdus");
                  SmsMessage[] messages = new SmsMessage[pdus.length];

                  for (int i = 0; i < pdus.length; i++)
                    messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);

                  for (SmsMessage message : messages)
                  {
                         Toast.makeText(ListenSMSservice.this, "isi pesan >> "+message.getDisplayMessageBody(), Toast.LENGTH_LONG).show();
                         receivedMessage(message.getDisplayOriginatingAddress());
                         if (message.getDisplayMessageBody().toString().equalsIgnoreCase("aaaa"))
                         {
                             Toast.makeText(ListenSMSservice.this, "messegenya aaaa", 1).show();
                         }
                  }
                }
              }
    }
};
private void receivedMessage(String message)
{
    messageList.add(message);
    adapter.notifyDataSetChanged();
}

@Override
public void onDestroy() {
    Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
}

不幸的是,我的服务可以通过高级任务杀手来停止,所以我无法收听短信指令。

我正在使用start_sticky选项,但它对我不起作用。
有没有人知道如何处理这个问题,所以即使使用任务杀手杀死我的应用程序,我也可以收听短信(自动启动服务)?

2 个答案:

答案 0 :(得分:10)

  

任何人都知道如何处理这个问题,所以我可以在任务杀手杀死我的应用程序时收听短信(自动启动服务)

如果用户通过“设置”或任务杀手强制停止您的应用程序,则用户会说您的应用程序不应该运行。请尊重并尊重这一决定。

从Android 3.1开始,您的应用程序在强制停止后不会再次运行,直到用户再次从活动中启动它。

答案 1 :(得分:4)

在清单中注册<reciver>,当您收到短信时,它会启动您的应用

<receiver android:name=".YourBroadCastReciver" android:enabled="true">
  <intent-filter>
    <action android:name="android.provider.Telephony.SMS_RECEIVED" />
  </intent-filter>
</receiver>