如何使用广播接收器获取运行的应用程序/包的名称 [英] how to get running application/package name using broadcast receiver

查看:337
本文介绍了如何使用广播接收器获取运行的应用程序/包的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用我想写的接收广播code,对于它的应用是running.like当我们运行/打开我的广播接收器应该接受它的任何应用程序。
我怎样才能获得应用程序名称/包名???

 公共类接收器扩展了BroadcastReceiver {@覆盖
公共无效的onReceive(上下文的背景下,意图意图){    如果(android.intent.action.PACKAGE_FIRST_LAUNCH.equals(intent.getAction()))
     的System.out.println(上下文...+ context.getPackageName());
}
 }

androidmanifest文件 -

 <接收
        机器人:名字=com.veg.app.Receiver
        机器人:启用=真        机器人:标签=StartReceiver>
        &所述;意图滤光器>
            <作用机器人:名字=android.intent.action.PACKAGE_FIRST_LAUNCH/>
            <类机器人:名字=android.intent.category.DEFAULT/>
        &所述; /意图滤光器>
    < /接收器>


解决方案

下面是一个使用活动管理做到这一点的好办法。你基本上获得活动管理的runningTasks。它总是先返回当前活动的任务。从那里,你可以得到topActivity。

  ActivityManager AM =(ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
 //从当前运行的任务的信息
     清单< ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);     Log.d(topActivity,当前活动::
             + taskInfo.get(0).topActivity.getClassName());     组件名componentInfo = taskInfo.get(0).topActivity;
   componentInfo.getPackageName();

您将需要以下权限上的表现:

 使用许可权的android:NAME =android.permission.GET_TASKS

和存在的没有这样的事件被调用的任何应用程序时发射。任何意图没有播出上的应用程序的开始,所以ü可能需要在后台运行的平行线程监视在顶。

in my application i want to write code of receiving broadcast for which application is running.like when we run/open any application my broadcast receiver should receive it. how can i get the app name/ package name???

 public class Receiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    if("android.intent.action.PACKAGE_FIRST_LAUNCH".equals(intent.getAction()))
     System.out.println("context..."+context.getPackageName());


}
 }

androidmanifest file-

  <receiver
        android:name="com.veg.app.Receiver"
        android:enabled="true"

        android:label="StartReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.PACKAGE_FIRST_LAUNCH" />      
            <category android:name="android.intent.category.DEFAULT"/> 
        </intent-filter>
    </receiver>

解决方案

Here's a good way to do it using the activity manager. You basically get the runningTasks from the activity manager. It will always return the currently active task first. From there you can get the topActivity.

ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
 // get the info from the currently running task
     List< ActivityManager.RunningTaskInfo > taskInfo = am.getRunningTasks(1); 

     Log.d("topActivity", "CURRENT Activity ::"
             + taskInfo.get(0).topActivity.getClassName());

     ComponentName componentInfo = taskInfo.get(0).topActivity;
   componentInfo.getPackageName();

You will need the following permission on your manifest:

uses-permission android:name="android.permission.GET_TASKS"

And there is no such event fired when any application is invoked. Any intent is not broadcasted on start of an application, so u might need a thread running parallely in background to monitor the top activity.

这篇关于如何使用广播接收器获取运行的应用程序/包的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆