2013-01-16 16 views
5

मैं एक एपीके फ़ाइल स्थापित करना चाहता हूं और इंस्टॉल स्थिति से संबंधित जानकारी को पकड़ने के लिए प्रसारण-रिसीवर सेट करना चाहता हूं।एंड्रॉइड: एप्लिकेशन इंस्टॉल/अनइंस्टॉल करने पर ब्रॉडकास्ट रिसीवर

मैं एक BroadcastReceiver वर्ग तैयार किया है:

public class newPackageReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     Log.d("DEBUG"," test for application install/uninstall"); 
    } 

} 

मुख्य गतिविधि में, मैं पहली बार, एक नया रिसीवर वस्तु रजिस्टर तो instanciate बटन आवेदन के लिए स्थापित करें।

public void onCreate(Bundle savedInstanceState) { 
... 
IntentFilter filter = new IntentFilter(); 
     filter.addAction(Intent.ACTION_PACKAGE_ADDED); 
     filter.addAction(Intent.ACTION_PACKAGE_CHANGED); 
     filter.addAction(Intent.ACTION_PACKAGE_DATA_CLEARED); 
     filter.addAction(Intent.ACTION_PACKAGE_INSTALL); 
     filter.addAction(Intent.ACTION_PACKAGE_REMOVED); 
     filter.addAction(Intent.ACTION_PACKAGE_REPLACED); 
     filter.addAction(Intent.ACTION_PACKAGE_RESTARTED); 

     receiver = new newPackageReceiver(); 
     registerReceiver(receiver, filter); 
     ... 

dlButton.setText(R.string.dl_button); 
dlButton.setOnClickListener(new AppliDownloadOnClickListener(this)); 


@Override 
public void onDestroy(){ 
    unregisterReceiver(receiver); 
    super.onDestroy(); 
} 

मेरी OnclickListener कक्षा में, मैं डाल:

@Override 
    public void onClick(View v) { 

    // actually, the below process is in an asyncTask 
    URL url; 
    Intent promptInstall; 

    try { 
     url = new URL(apkurl); 

     HttpURLConnection c = (HttpURLConnection) url.openConnection(); 
     c.setRequestMethod("GET"); 
     c.setDoOutput(true); 
     c.connect(); 

     String PATH = Environment.getExternalStorageDirectory()+ "/download/"; 
     File file = new File(PATH); 
     file.mkdirs(); 
     File outputFile = new File(file, "app.apk"); 
     FileOutputStream fos = new FileOutputStream(outputFile); 

     InputStream is = c.getInputStream(); 

     byte[] buffer = new byte[1024]; 
     int len1 = 0; 
     while ((len1 = is.read(buffer)) != -1) { 
      fos.write(buffer, 0, len1); 
     } 

     fos.close(); 
     is.close(); 

     promptInstall = new Intent(Intent.ACTION_VIEW); 
     promptInstall.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/" + "app.apk")), "application/vnd.android.package-archive"); 

     if (promptInstall != null) { 
      activity.startActivity(promptInstall); 
     } else { 
      ErrorDetails.displayToastMessage(activity,R.string.connection_error); 
     } 


    } catch (...) { 
     ... 
    } 

} 

ऊपर कोड (मैं इसे सिकुड़ गए हैं), बटन क्लिक होने, संस्थापक प्रदर्शित किया जाता है और आवेदन पूरी तरह से स्थापित किया गया है, लेकिन रिसीवर वर्ग के साथ (newPackageReceiver) कभी नहीं कहा जाता है। पंजीकरण (रजिस्टर रिसीवर) ऑनक्रेट विधि में किया जाता है और अनियंत्रित रिसीवर को ऑनस्ट्राय विधि में बुलाया जाता है, इसलिए यह वैध हो जाता है। तुम जानते हो क्यों ?

पढ़ने के लिए धन्यवाद!

उत्तर

5

आपको अपने इरादे फ़िल्टर में data scheme जोड़ने की आवश्यकता है।

filter.addDataScheme("package"); 

इसके अलावा, ACTION_PACKAGE_INSTALL कभी भी उपयोग में नहीं था।

+0

बिल्कुल सही ~ यह काम करता है! डेटा योजना के लिए धन्यवाद और ACTION_PACKAGE_INSTALL ~ – johann

+0

के बारे में जानकारी ठीक काम करती है, लेकिन क्यों? – aotian16

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