2013-01-16 44 views
5

Tôi muốn cài đặt tệp APK và đặt thiết bị phát sóng để nhận thông tin liên quan đến trạng thái cài đặt.Android: BroadcastReceiver khi cài đặt/gỡ cài đặt ứng dụng

tôi đã chuẩn bị một lớp BroadcastReceiver:

public class newPackageReceiver extends BroadcastReceiver { 

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

} 

Trong hoạt động chính, đầu tiên tôi đăng ký một đối tượng thu mới, sau đó nút instanciate cho các ứng dụng cài đặt.

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

Trong lớp OnClickListener của tôi, tôi đặt:

@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 (...) { 
     ... 
    } 

} 

Với đoạn mã trên (Tôi đã bị thu hẹp nó), khi nút được nhấn vào, trình cài đặt sẽ được hiển thị và ứng dụng được cài đặt một cách hoàn hảo, nhưng lớp nhận (newPackageReceiver) không bao giờ được gọi. Việc đăng ký (registerReceiver) được thực hiện trong phương thức onCreate và unregisterReceiver được gọi trong phương thức onDestroy, vì vậy nó sẽ có giá trị. Bạn có biết tại sao ?

Cảm ơn bạn đã đọc!

Trả lời

5

Bạn cần thêm data scheme vào bộ lọc ý định của mình.

filter.addDataScheme("package"); 

Ngoài ra, ACTION_PACKAGE_INSTALL chưa bao giờ được sử dụng.

+0

Perfect ~ Nó hoạt động! Cảm ơn bạn về sơ đồ dữ liệu và thông tin về ACTION_PACKAGE_INSTALL ~ – johann

+0

Hoạt động tốt, nhưng tại sao? – aotian16

Các vấn đề liên quan