本文介绍了杀应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想经过10秒杀我的应用程序,我要开始的服务,让杀招后杀应用程序。但是,这code后10秒关注。为什么呢?

  AlarmManager alarmMgr =(AlarmManager)getSystemService(Context.ALARM_SERVICE);
意向意图=新意图(这一点,service_MyService.class);
intent.setAction(杀);
的PendingIntent的PendingIntent = PendingIntent.getBroadcast(这一点,0,意向,0);
日历时间= Calendar.getInstance();
time.setTimeInMillis(System.currentTimeMillis的());
time.add(Calendar.SECOND,10);
alarmMgr.set(AlarmManager.RTC_WAKEUP,time.getTimeInMillis(),的PendingIntent);


解决方案

在您的code您发送一个广播意图

  PendingIntent.getBroadcast(这一点,0,意向,0);

这只能启动一个BroadcastReceiver,而不是服务。但是,你正在使用正确的意图,因为AlarmManager无法叫醒一个服务,只有广播接收器-S。

您应该创建一个BroadcastReceiver,而不是一种服务,替换此行:

 意向意图=新意图(这一点,service_MyService.class);

这一行

 意向意图=新意图(这一点,KillerBroadCastReceiver.class);

,然后在你的类KillerBroadCastReceiver你杀的应用程序。

不过,如果你想杀死一段时间后应用,更好的解决方案是使用一个处理器。下面是一个例子:

 公共类VeryFirstActivity延伸活动{
    处理器mKillerHandler;    公共无效的onCreate(...){
        mKillerHandler =新的处理程序();
        mKillerHandler.postDelayed(新的Runnable(){
           公共无效的run(){
               VeryFirstActivity.this.finish();
           }
        },10000); //10秒
    }
}

I want to kill my app after 10 second, I want to start service that kill app after getting Kill Action. But this code noting after 10 second. Why?

AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, service_MyService.class);
intent.setAction("Kill");
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,intent, 0);
Calendar time = Calendar.getInstance();
time.setTimeInMillis(System.currentTimeMillis());
time.add(Calendar.SECOND, 10);
alarmMgr.set(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(),pendingIntent);
解决方案

In your code you are sending a broadcast intent

PendingIntent.getBroadcast(this, 0,intent, 0);

Which can only start a BroadCastReceiver and not a service. However, you are using the right intent, because AlarmManager can not wake up a services, only BroadCastReceiver-s.

You should create a BroadCastReceiver instead of a service, and replace this line:

Intent intent = new Intent(this, service_MyService.class);

with this line

Intent intent = new Intent(this, KillerBroadCastReceiver.class);

and then in your KillerBroadCastReceiver class you kill the app.

However if you want to kill the app after some time, a better solution is to use a Handler. Here is an example:

public class VeryFirstActivity extends Activity {
    Handler mKillerHandler;

    public void onCreate(...) {
        mKillerHandler = new Handler();
        mKillerHandler.postDelayed(new Runnable() {
           public void run() {
               VeryFirstActivity.this.finish();
           }
        }, 10000); // 10 seconds
    }
}

这篇关于杀应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 12:39