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文件中。 –

相關問題