2016-09-19 44 views
1

我想使用接收器进行自动更新。如何在Android中设置Receiver?

点击时,使用URI获取下载。

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(strUrl)); 
startActivity(intent); 

而我想要的是运行由intent自动下载的apk。

现在,我必须在下载后再点击apk文件。但我想自动下载 - 运行新的Apk - 删除apk文件。

所以我试图使用接收器,但我不知道如何使用它。

1,我在清单中加入了这个。

<receiver android:name=".common.PackageReceiver" > 
    <intent-filter> 
     <action android:name="android.intent.action.PACKAGE_ADDED" /> 
     <action android:name="android.intent.action.PACKAGE_REMOVED" /> 
     <action android:name="android.intent.action.PACKAGE_REPLACED" /> 
     <action android:name="android.intent.action.DOWNLOAD_COMPLETE" /> 
     <data android:scheme="package" /> 
    </intent-filter> 
</receiver> 

2,我做新课。

package com.ezcaretech.ecf.common; 
public class PackageReceiver extends BroadcastReceiver { 

public static final String DOWNLOAD_COMPLETE = "android.intent.action.DOWNLOAD_COMPLETE"; 

@Override 
public void onReceive(Context context, Intent intent) { 
    String packageName = intent.getData().getSchemeSpecificPart(); 
    String action = intent.getAction(); 

    if (action.equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) { 
     Log.d("TAG", "DOWNLOAD COMPLETE"); 
    } 
    } 
} 

但是,下载后,接收器不再工作。

感谢

+0

它可以帮助你检查

+1

发表您的下载管理器代码 –

+0

你有PackageReceiver.DOWNLOAD_COMPLETE和DownloadManager.ACTION_DOWNLOAD_COMPLETE,你使用哪一个? – HendraWD

回答

0

你已静态注册这就保证了任何你贴的意图被发送时,你的应用程序将被启动的BroadcastReceiver

然而,接收机仅做一些事情时,它得到

DownloadManager.ACTION_DOWNLOAD_COMPLETE

+0

确实是!谢谢 – Adrian

0

谢谢你们。我改变我的代码

builder.setPositiveButton(R.string.kor_confirm, new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int whichButton) { 
      DownloadManager dm= (DownloadManager) getSystemService(DOWNLOAD_SERVICE); 
      DownloadManager.Request request = new DownloadManager.Request(Uri.parse(strUrl)); 
      dm.enqueue(request); 
     } 
    });` 

它运作良好。感谢所有