Last active
November 7, 2022 03:54
-
-
Save georgioupanayiotis/8f4b7ea7007a8cb81a51 to your computer and use it in GitHub Desktop.
List running service of Android device
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
List running service of Android device |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.runningservices.panayiotisgeorgiou.runningservices; | |
import android.app.Activity; | |
import android.app.ActivityManager; | |
import android.os.Bundle; | |
import android.text.method.ScrollingMovementMethod; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.widget.TextView; | |
import java.util.List; | |
public class MyActivity extends Activity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_my); | |
//setContentView(R.layout.activity_main); | |
ActivityManager activityManager = (ActivityManager)getSystemService(ACTIVITY_SERVICE); | |
/* | |
* maxNum: the maximum number of entries to return in the list. | |
* The actual number returned may be smaller, | |
* depending on how many services are running. | |
*/ | |
int maxNum = 100; | |
List<ActivityManager.RunningServiceInfo> list = activityManager.getRunningServices(maxNum); | |
StringBuilder info = new StringBuilder(); | |
info.append("Services currently running: " + list.size() + "\n\n"); | |
for(int i=0; i<list.size(); i++){ | |
info.append(list.get(i).service + "\n\n"); | |
} | |
TextView texView = new TextView(this); | |
texView.setMovementMethod(new ScrollingMovementMethod()); | |
texView.setText(info); | |
setContentView(texView); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.my, menu); | |
return true; | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
// Handle action bar item clicks here. The action bar will | |
// automatically handle clicks on the Home/Up button, so long | |
// as you specify a parent activity in AndroidManifest.xml. | |
int id = item.getItemId(); | |
if (id == R.id.action_settings) { | |
return true; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment