开发者

How to create a service in Android that can't be killed by Advanced Task Killer

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.

Here's the main form:

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 st开发者_StackOverflow社区ub
            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;
    }
}

and this is the Service form:

@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();
}

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

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?


anyone have idea how to handle that problem, so i can listen to the SMS (auto started service) while task killer have killed 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.

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


Register a <reciver> in the manifest and it will start your app when you receive a SMS

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜