本文介绍了如何在Android中创建高级任务杀手无法杀死的服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个安全应用程序,就像Mcafee wave secure.
我的应用程序正在侦听SMS命令,并在命令匹配时执行一些操作,因此我创建了带有另一个服务的表单以侦听SMS.

I create a security application just like Mcafee wave secure.
My application is listening for SMS command and doing some action when the command is match, so I create a form with another service for listening the 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();
}

不幸的是,高级任务杀手可以停止我的服务,所以我听不到SMS命令.

Unfortunately, my service can be stopped by Advanced task killer, so I can't listen for the SMS command.

我正在使用 start_sticky 选项,但是它对我不起作用.
有谁知道如何处理该问题,因此即使使用任务杀手杀死我的应用程序,我也可以收听SMS(自动启动服务)?

I'm using start_sticky option, but it does not work for me.
Does anyone have an idea how to handle that problem, so I can listen to the SMS (auto started service) even when task killer was used to kill my application?

推荐答案

如果用户通过设置"或任务杀手"强制停止您的应用程序,则表示您的应用程序不应运行.请尊重并尊重该决定.

If the user force-stops your application via Settings or a task killer, the user is saying that your app is not supposed to run. Please honor and respect that decision.

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

As of Android 3.1, your application will not run again after being force-stopped, until the user launches it again from an activity.

这篇关于如何在Android中创建高级任务杀手无法杀死的服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 14:42
查看更多