Android Get Running and Kill application example
This post about how to kill running application in mobile
Below i given example .
Create listview and custom adapter layout.
myactivity.xml
customadapter.xml
following permission add in manifest
Activity Code
CustomAdapterClassCode
After kill the apps
Manifest
Structure class
Download Source Code Here
This post about how to kill running application in mobile
Below i given example .
Create listview and custom adapter layout.
myactivity.xml
<?xml version="1.0"
encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ListView
android:id="@+id/appList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/kilBtn"
android:background="#636260"
android:horizontalSpacing="1dp"
android:verticalSpacing="1dp"
>
</ListView>
<Button
android:id="@+id/kilBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="KillAllRunningApps"
/>
</RelativeLayout>
customadapter.xml
<?xml version="1.0"
encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#f49f9f"
>
<ImageView
android:id="@+id/appIcon"
android:layout_width="48dip"
android:layout_height="48dip"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="@drawable/ic_launcher"
/>
<TextView
android:id="@+id/applicationName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/appIcon"
android:text=""
/>
</RelativeLayout>
following permission add in manifest
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/>
<uses-permission android:name="android.permission.GET_TASKS"/>
Activity Code
package
com.example.killrunningapps;
import java.util.ArrayList;
import java.util.List;
import
android.app.Activity;
import
android.app.ActivityManager;
import
android.app.ActivityManager.RunningTaskInfo;
import
android.content.pm.PackageManager;
import
android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.view.View;
import
android.view.View.OnClickListener;
import
android.widget.Button;
import
android.widget.ListView;
public class MainActivity extends Activity {
ArrayList<AppStructure> appStructurelist = new
ArrayList<AppStructure>();
AppStructure appStructure;
ListView listView;
List<RunningTaskInfo> runningAPps;
Button killApps;
ActivityManager activityManager;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myactivity);
listView = (ListView)
findViewById(R.id.appList);
killApps =
(Button)findViewById(R.id.kilBtn);
activityManager = (ActivityManager) this
.getSystemService(ACTIVITY_SERVICE);
runningAPps = activityManager
.getRunningTasks(Integer.MAX_VALUE);
for (int i = 0; i < runningAPps.size(); i++) {
appStructure = new AppStructure();
try {
appStructure.appName =(String)
getPackageManager().getApplicationLabel(getPackageManager()
.getApplicationInfo(runningAPps.get(i).topActivity.getPackageName(),
PackageManager.GET_META_DATA));
appStructure.appIcons =
getPackageManager().getApplicationIcon(runningAPps.get(i).topActivity.getPackageName());
} catch
(NameNotFoundException e) {
e.printStackTrace();
}
appStructurelist.add(appStructure);
}
killApps.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
for (int i = 0; i < runningAPps.size(); i++) {
if(!runningAPps.get(i).topActivity.getPackageName().equalsIgnoreCase("com.example.retriveinstallapps")){
activityManager.killBackgroundProcesses(runningAPps.get(i).topActivity.getPackageName());
}
try {
appStructure.appName =(String)
getPackageManager().getApplicationLabel(getPackageManager()
.getApplicationInfo(runningAPps.get(i).topActivity.getPackageName(),
PackageManager.GET_META_DATA));
appStructure.appIcons =
getPackageManager().getApplicationIcon(runningAPps.get(i).topActivity.getPackageName());
} catch
(NameNotFoundException e) {
e.printStackTrace();
}
appStructurelist.clear();
appStructurelist.add(appStructure);
}
updateAdpater();
}
});
updateAdpater();
}
public void updateAdpater(){
InstalledAppAdapter adapter = new
InstalledAppAdapter(MainActivity.this,
appStructurelist);
listView.setAdapter(adapter);
}
}
CustomAdapterClassCode
import java.util.ArrayList;
import
android.app.Activity;
import
android.view.LayoutInflater;
import android.view.View;
import
android.view.ViewGroup;
import
android.widget.BaseAdapter;
import
android.widget.ImageView;
import
android.widget.TextView;
public class InstalledAppAdapter extends BaseAdapter {
Activity activity;
ArrayList<AppStructure> appStrucutreList;
public InstalledAppAdapter(Activity act,
ArrayList<AppStructure> appStructure) {
this.activity = act;
this.appStrucutreList = appStructure;
}
@Override
public int getCount() {
// TODO Auto-generated
method stub
return appStrucutreList.size();
}
@Override
public Object getItem(int arg0) {
return arg0;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated
method stub
return arg0;
}
@Override
public View getView(int arg0, View view,
ViewGroup arg2) {
LayoutInflater inflator
=(LayoutInflater)activity.getSystemService(activity.LAYOUT_INFLATER_SERVICE);
view = inflator.inflate(R.layout.adapter_layout, null);
TextView appName =
(TextView)view.findViewById(R.id.applicationName);
ImageView appIcon =
(ImageView)view.findViewById(R.id.appIcon);
appName.setText(appStrucutreList.get(arg0).appName);
appIcon.setBackgroundDrawable(appStrucutreList.get(arg0).appIcons);
return view;
}
}
After kill the apps
Manifest
<?xml version="1.0"
encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.killrunningapps"
android:versionCode="1"
android:versionName="1.0"
>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19"
/>
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/>
<uses-permission android:name="android.permission.GET_TASKS"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
>
<activity
android:name="com.example.killrunningapps.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>
</application>
</manifest>
Structure class
import
android.graphics.drawable.Drawable;
public class AppStructure {
public String appName;
public Drawable appIcons;
}
Download Source Code Here
No comments:
Post a Comment