In android phone I wish to toast the name of the opened package(app) Again when I open another app I wish the package name of that particular app is toast. Also I wish to do all this using service I would be very thankful for Your help.
Asked
Active
Viewed 305 times
0
-
What do you mean by "toast something" – Bálint Sep 28 '16 at 12:50
-
means to make a toast that something? – Benny Huo Sep 28 '16 at 12:57
-
1Is this helpful? http://stackoverflow.com/questions/2166961/determining-the-current-foreground-application-from-a-background-task-or-service – GoneUp Sep 28 '16 at 13:07
-
there is no trigger you can use to know when an app is opened unless your app is a launcher app – tyczj Sep 28 '16 at 13:09
-
Thank You,i could not find exact codes but thankyou so much for reply – Ank Sep 28 '16 at 18:55
2 Answers
1
Use the below code in your launcher activity
public static String PACKAGE_NAME;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
PACKAGE_NAME = getApplicationContext().getPackageName();
Toast.makeText(this, PACKAGE_NAME , Toast.LENGTH_SHORT).show();
}

nishith kumar
- 981
- 7
- 20
-1
So you need to show package name of any app you open. First, search how to create a broadcast receiver then put this code in it.
ActivityManager mgr = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
List< ActivityManager.RunningTaskInfo > taskInfo = mgr.getRunningTasks(1);
Log.d("topActivity", "Current Running Activity ::"
+ taskInfo.get(0).topActivity.getClassName());
ComponentName component = taskInfo.get(0).topActivity;
String packageName = component.getPackageName();
Toast.makeText(getApplicationContext(), packageName , Toast.LENGTH_SHORT).show();
Add this permission on your manifest:
uses-permission android:name="android.permission.GET_TASKS"
To detect if an app is opened , you have to create an intent and fire it when the top activity is changed from your background thread.

A.J
- 726
- 1
- 7
- 19
-
-
Thank You so much.I am new to android cause of which I dont know how to use broadcast receiver create intent to fire in background thread can you please add this(broadcast receiver) program too in your previous program.Thank You – Ank Sep 28 '16 at 18:31