0

问题:广播android.intent.action.DOWNLOAD_COMPLETE只有在应用程序正在运行或后台收到。如果应用程序被杀死,那么广播从未收到广播DOWNLOAD_COMPLETE未收到,如果应用程序未运行

AndroidManifest.xml中

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /> 
<uses-permission android:name="android.permission.ACCESS_DOWNLOAD_MANAGER" /> 
<uses-permission android:name="android.permission.SEND_DOWNLOAD_COMPLETED_INTENTS" /> 

<receiver 
    android:name=".adapters.VideoListAdapter$VideoDownloadedReceiver" 
    android:exported="true"> 

    <intent-filter> 
     <action android:name="android.intent.action.DOWNLOAD_COMPLETE" /> 
    </intent-filter> 
</receiver> 

Receiver类

public static class VideoDownloadedReceiver extends BroadcastReceiver implements AsyncResponse { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     Log.i("YES", "in receive"); 
    } 
} 

请注意,我不是面临这个问题中的所有设备。

设备上,我现在面临这个问题:Lenevo A600华硕Zenfone最大

设备上,它的正常工作:华硕Zenfone 5(CyanogenMod的13),机器人工作室模拟器(的Nexus 6P棉花糖),三星J7总理,三星J5,Nexus 5的

+0

请参阅我的回答HTTPS ://stackoverflow.com/a/44415369/6548766 –

回答

0

我想原因可能是由于定制ROM,限制了广播,CM,ASOP,是原来SYSTE m.so ,,,

如果你想收到后,即使关闭应用程序,那么你应该使用的广播服务

以下将帮助你实现广播

0
+0

我创建了一个服务,并取得了其广播但仍ð如果应用程序被终止,则不会调用onReceive方法。如果应用正在运行,则调用服务onReceive方法 –

0

接收器似乎是一个内部类。由于该应用程序已被销毁,因此您的内部接收器类是已销毁类的一部分。

我立足这样的假设是因为您的清单具有

android:name=".adapters.VideoListAdapter$VideoDownloadedReceiver"

分离的“广播接收器”到自己的类,并预期它应该工作。

至于在某些手机上的工作,我会检查的其他应用程序运行的内容和应用什么样的应用程序的抑制,您可以在其中可能是导致强制关闭您的应用程序在后台运行了。

,你应该有这样的事情。我看着代码和你嵌套receeiver但你不持有该服务。

调用函数来启动动作

private void startDownloadService() { 
    Intent downloadIntent = new Intent(this, VideoDownloadReceiverService.class); 

    if (!VideoDownloadReceiverService.isRunning()) { 
     // My manifest has enabled="false" so i do this to turn it 'ON' 
     component = new ComponentName(CounterActivity.this, VideoDownloadReceiverService.class); 
     getPackageManager() 
       .setComponentEnabledSetting(component, 
         PackageManager.COMPONENT_ENABLED_STATE_ENABLED, 
         PackageManager.DONT_KILL_APP); 
     // Start the service 
     startService(downloadIntent); 
    } 
} 
在这里单独的文件

是服务

public class VideoDownloadReceiverService extends Service{ 

    private static boolean isRunning = false; 

    public VideoDownloadReceiverService(){ 

    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 

     // you may not need this since oyu register it in the Manifest. the fact the service is running 
     // should keep your app alive and receiving and unable to be shutdown 
     // but this is what i did 
     IntentFilter filter = new IntentFilter(); 
     filter.addAction(DownloadManager.ACTION_DOWNLOAD_COMPLETE); 
     receiver = new VideoDownloadReceiver(); 
     registerReceiver(receiver, filter); 

     Notification notification = new NotificationCompat.Builder(this) 
       .setSmallIcon(R.drawable.ic_launcher) 
       .setContentText("Viewer Text In Notification").build(); 
     startForeground(MyConstants.NOTIFICATION_ID, notification); 

     isRunning = true; 
     return START_STICKY; // This is the line that keeps the service running no matter what 
    } 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
    } 

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

在单独的文件中,这里是接收器

public class VideoDownloadReceiver extends BroadcastReceiver { 
    public VideoDownloadReceiver() { 

    } 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     PowerManager powerManager = (PowerManager) context.getSystemService(POWER_SERVICE); 
     PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, 
       "com.example.johnbravado.zionwork"); 
     wakeLock.acquire(); 

     Toast.makeText(context, "in service", Toast.LENGTH_SHORT).show(); 
     Log.i("YES", "service on receive"); 

     wakeLock.release(); 
    } 
} 
+0

对广播接收方做出了不同的调用,但仍然没有成功。 –

+0

你能告诉我你是如何使sendBoradcast –

+0

发送广播,我正在注册在manifex文件中。 –