【问题标题】:How to create a service in Android that can't be killed by Advanced Task Killer如何在 Android 中创建一个高级任务杀手无法杀死的服务
【发布时间】:2011-10-14 10:45:06
【问题描述】:

我创建了一个安全应用程序,就像 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 选项,但它对我不起作用。
有谁知道如何处理这个问题,所以即使任务杀手被用来杀死我的应用程序,我也可以收听 SMS(自动启动服务)?

【问题讨论】:

  • 如果用户通过 3rd 方工具或设置菜单强制停止您的应用程序,那么他们希望将其停止。
  • 是的,当然.. 但我做了一个安全应用程序。如果设备被盗或丢失,我的应用程序应该可以工作。我的应用程序可以锁定安卓设备,这样小偷就无法访问手机。如果我的服务可以停止,如何防止丢失的手机被小偷访问?有什么解决办法??谢谢..
  • 您声称正在提供“安全服务”。您可能正在创建恶意软件,因为恶意软件可能具有您描述的功能。 Android 可以保护用户免受恶意软件的侵害,这意味着它可以保护用户免受您的“安全服务”的侵害。有更好的解决方案可以保护用户免受属于(或可能属于)操作系统一部分的手机丢失/被盗/扣押(例如,整个磁盘加密)。
  • 这是我的最后一个项目。我创建了一个像 mcafee wave secure 这样的应用程序。如果你说我创建了一个恶意软件,那么 mcafee wave secure 怎么样??

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


【解决方案1】:

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

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

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

【讨论】:

  • “如果用户通过设置或任务杀手强制停止您的应用程序,则用户说您的应用程序不应该运行。请尊重并尊重该决定。” >> 我做了一个安全应用程序,可以在安卓手机丢失或被盗时锁定它,所以小偷无法访问那部手机..如果我的应用程序可以停止,那么我如何保护安卓手机?有什么解决办法吗?
  • @CommonsWare 您能否添加来自 android 网站的链接引用:“从 Android 3.1 开始,您的应用程序在被强制停止后将不会再次运行,直到用户从活动中再次启动它”
【解决方案2】:

在清单中注册&lt;reciver&gt;,它会在您收到短信时启动您的应用

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

【讨论】:

  • 这对 OP 的问题没有帮助,从 Android 3.1 开始。在用户启动活动之前,强制停止的应用程序不会响应广播。
  • @CommonsWare 活动第一次启动后会正常接收广播吗?
  • @opc0de:是的,除非用户进入设置并点击应用程序的“强制停止”。然后您将恢复到首次安装应用程序时的状态。
猜你喜欢
  • 1970-01-01
  • 2019-08-24
  • 2023-03-16
  • 2013-12-31
  • 1970-01-01
  • 2015-12-13
  • 1970-01-01
  • 2014-07-19
  • 2019-05-19
相关资源
最近更新 更多