How to kill android application using android code?
Posted
by
Natarajan M
on Stack Overflow
See other posts from Stack Overflow
or by Natarajan M
Published on 2014-05-28T09:09:48Z
Indexed on
2014/05/28
9:26 UTC
Read the original article
Hit count: 245
android
I am develoing small android application in eclipse. In that project i kill the running process in android, i got the Permission Denial error. how can i solve this problem in android. Anybody help for this problem.... THIS IS MY CODE
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;
}
}
After executing the code. i got the following error...
Permission Denial:
killBackgroundProcess() from pid=894,
uid=10052 requires
android.permission.KILL_BACKGROUND_PROCESSES
Also i put the following line on manifest file
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESS" />
Anybody help for how to solve this problem... Thanking you....
© Stack Overflow or respective owner