如何使用Android代码杀死Android应用程序?

时间:2014-05-28 09:09:48

标签: android

我正在开发eclipse中的小型android应用程序。在该项目中我杀死了android中的运行进程,我得到了Permission Denial错误。我怎么能在android中解决这个问题。 有人帮忙解决这个问题.... 这是我的代码

package com.example.nuts;

import java.util.Iterator;
import java.util.List;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.widget.Toast;
import android.*;

public class killprocess extends Activity
{
    SmsManager smsManager = SmsManager.getDefault();    
Recivesms rms=new Recivesms();
String Number="";
int pid=0;
String appname="";

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);     
    try
    {           
        Number=Recivesms.senderNum;
        pid=Integer.parseInt(Recivesms.struid);
        appname=getAppName(pid);
        Toast.makeText(getBaseContext(),"App Name is "+appname, Toast.LENGTH_LONG).show();  
         ActivityManager am = (ActivityManager) getSystemService(Activity.ACTIVITY_SERVICE);
            List<RunningAppProcessInfo> processes = am.getRunningAppProcesses();
            if (processes != null){
                for (int i=0; i<processes.size(); i++){
                    RunningAppProcessInfo temp = processes.get(i);
                    String pName = temp.processName;

                    if (pName.equals(appname))
                    {
                        Toast.makeText(getBaseContext(),"App Name is matched  "+appname+" "+pName, Toast.LENGTH_LONG).show();   
                            int pid1 = android.os.Process.getUidForName(pName);
                            //android.os.Process.killProcess(pid1);
                            am.killBackgroundProcesses(pName);
                            Toast.makeText(getBaseContext(), "Killed successfully....", Toast.LENGTH_LONG).show();
                    }
                    }
             }
    smsManager.sendTextMessage(Number, null,"Your process Successfully killed..." , null,null);     
    }catch(Exception e)
    {
        Toast.makeText(getBaseContext(),e.getMessage(), Toast.LENGTH_LONG).show();
    }

}
    private String getAppName(int Pid)
    {
        String processName = "";
        ActivityManager am = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
        List l = am.getRunningAppProcesses();
        Iterator i = l.iterator();
        PackageManager pm = this.getPackageManager();
        while(i.hasNext()) 
        {
                  ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo)(i.next());
          try 
          { 
              if(info.pid == Pid)
              {
                  CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName, PackageManager.GET_META_DATA));
                  //Log.d("Process", "Id: "+ info.pid +" ProcessName: "+    info.processName +"  Label: "+c.toString());
                          //processName = c.toString();
                      processName = info.processName;
                  }
              }
              catch(Exception e) 
              {
                    //Log.d("Process", "Error>> :"+ e.toString());
              }
       }
        return processName;
    }
 }

执行代码后。我收到了以下错误......

Permission Denial:
killBackgroundProcess() from pid=894,
uid=10052 requires
android.permission.KILL_BACKGROUND_PROCESSES

此外,我将以下行放在清单文件

  <uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESS" />

有人帮忙解决这个问题...... 感谢你......

这是我的清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.nuts"
android:versionCode="1"
android:versionName="1.0" >
 <uses-permission android:name="android.permission.READ_SMS" />
 <uses-permission android:name="android.permission.WRITE_SMS" />
  <uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESS" ></uses-permission>


<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
 <uses-permission android:name="android.permission.SEND_SMS"></uses-permission>


<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/clip1"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.nuts.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.nuts.createpassword"
        android:label="@string/app_name" >

    </activity>
    <activity
        android:name="com.example.nuts.editpassword"
        android:label="@string/app_name" >

    </activity>

    <activity
        android:name="com.example.nuts.processList"
        android:label="@string/app_name" >

    </activity>
    <activity
        android:name="com.example.nuts.killprocess"
        android:label="@string/app_name" 

         >

    </activity>
   <receiver android:name=".Recivesms">   
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>
    <activity
        android:name="com.example.nuts.sensSms"
        android:label="@string/app_name" >

    </activity>



</application>

4 个答案:

答案 0 :(得分:0)

将此权限添加到您的清单文件以杀死后台进程

    <uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />

注意:如果您在Android上获得所有正在运行的应用程序,那么您只能杀死手动安装的应用程序,如地铁冲浪者,但你不能杀死内置的Android应用程序,如Android的启动器。

答案 1 :(得分:0)

在清单文件中添加权限

<manifest xlmns:android...>
 ...
 <uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"></uses-permission>
</manifest>

答案 2 :(得分:0)

转到您的清单文件并添加此

<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/>

答案 3 :(得分:0)

您需要在AndroidManifest.xml

中添加相关权限

来自官方documentation

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.app.myapp" >
    <uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />
    ...
  </manifest>