2013-01-08 16 views
7

मैं एक समाधान है कि मैं अपने device.I पर उपयोगकर्ता ने खुली या स्थापित एप्लिकेशन प्राप्त करना चाहते हैं रहा हूँ इस प्रकार है जो एप्लिकेशन मेरी BroadcastReceiver class.I से उपयोगकर्ता द्वारा खोला गया मेरी कोड लागू कर दिया है प्राप्त करने के लिए:उपयोगकर्ता वर्तमान खोला आवेदन कैसे प्राप्त करें?

public class AppReceiver extends BroadcastReceiver{ 

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

    ActivityManager am = (ActivityManager)context.getSystemService(context.ACTIVITY_SERVICE); 
    List l = am.getRunningAppProcesses(); 
    Iterator i = l.iterator(); 
    PackageManager pm = context.getPackageManager(); 
    while(i.hasNext()) { 
     ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo)(i.next()); 
     try { 

     CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName, PackageManager.GET_META_DATA)); 
     Log.v(" App Name : ", c.toString()); 


     }catch(Exception e) { 
     } 
    } 


} 

<receiver android:name="AppReciever"> 
     <intent-filter> 
      <category android:name="android.intent.category.DEFAULT" /> 
      <action android:name="android.intent.action.PACKAGE_ADDED"></action> 
      <action android:name="android.intent.action.PACKAGE_CHANGED"></action> 
      <action android:name="android.intent.action.PACKAGE_INSTALL"></action> 
      <action android:name="android.intent.action.PACKAGE_REPLACED"></action> 
      <action android:name="android.intent.action.PACKAGE_RESTARTED"></action> 
    <data android:scheme="package" /> 

    </intent-filter> 
    </receiver> 

ऊपर कोड AppReciver Log.v पर ऐप्लिकेशन का नाम पाने के लिए क्रियान्वित नहीं है जब मैं पर जो पहले से ही अस्तित्व में रहे हैं नए एप्लिकेशन (स्थापित) खोला से:

मैं भी रूप में मैनिफ़ेस्ट फ़ाइल पर इस रिसीवर के बारे में जोड़ लिया है डिवाइस। यह डिवाइस पर केवल एक बार अन्य ऐप चलाने के लिए काम कर रहा है।

कृपया किसी भी शरीर मुझे BroadcastReceiver

+0

मैं हो रही है ऐप्लिकेशन का नाम केवल नए package_added लेकिन मैं अगर package_restarted/package_install/package_replaced/package_restarted –

+0

मैं माफी चाहता हूँ मैं तुम्हें क्या कह रहे हैं नहीं मिल सकता है प्राप्त करने के लिए , क्या आप मेरे लिए अपना प्रश्न दोबारा कर सकते हैं? क्या आप सभी इंस्टॉल किए गए ऐप्स प्राप्त करना चाहते हैं? –

उत्तर

2

से वर्तमान खोला अनुप्रयोगों पाने पर मदद से आप में जोड़े सेवा registerReceiver()। रिसीवर को अपंजीकृत करना न भूलें;

AppReceiver appReceiver = new AppReceiver(); 
    IntentFilter intentFilter = new IntentFilter(); 
    intentFilter.setPriority(900); 
    intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED); 
    intentFilter.addAction(Intent.ACTION_PACKAGE_CHANGED); 
    intentFilter.addAction(Intent.ACTION_PACKAGE_INSTALL); //@deprecated This constant has never been used. 
    intentFilter.addAction(Intent.ACTION_PACKAGE_REPLACED); 
    intentFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED); 
    registerReceiver(appReceiver, intentFilter); 

का पंजीकरण रद्द करने के लिए: unregisterReceiver(appReceiver);

+0

क्या प्राथमिकता Integer.MAX_VALUE का उपयोग करने में कोई हानि है? – gonzobrains

+1

SYSTEM_HIGH_PRIORITY मान के लिए IntentFilter क्लास दस्तावेज़ से: अनुप्रयोगों को कभी भी इस या उच्च प्राथमिकताओं के साथ फ़िल्टर का उपयोग नहीं करना चाहिए। – resource8218

संबंधित मुद्दे