0

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.

fabian
  • 80,457
  • 12
  • 86
  • 114
Ank
  • 13
  • 2

2 Answers2

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
  • He wants to get his package name, what's the great code for? – Ali Bdeir Sep 28 '16 at 16:07
  • 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