简体   繁体   中英

how to toast the opened app package name(when I open any app its package name needs to be toast

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.

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();
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM