Broadcast DOWNLOAD_COMPLETE is not recieved if app is not running - android

Issue: broadcasts android.intent.action.DOWNLOAD_COMPLETE is received only if application is running or in background. If application is killed then broadcast never received.
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 class
public static class VideoDownloadedReceiver extends BroadcastReceiver implements AsyncResponse {
#Override
public void onReceive(Context context, Intent intent) {
Log.i("YES", "in receive");
}
}
Please note the I am facing this issue not in all devices.
Devices on which I am facing this issue: Lenevo A600, Asus Zenfone Max
Device on which it's working fine: Asus Zenfone 5 (cyanogenmod 13), Android Studio Emulator (Nexus 6p marshmallow), Samsung J7 Prime , Samsung j5, Nexus 5

I think the reason may be due to the customization ROM ,limited the broadcast,CM,ASOP,is the originally System.so,,,

if you want to receive broadcast even after app closed then you should use broadcast in service
following will help you to implement:-
https://stackoverflow.com/a/16824692/4246910

The receiver appears to be an internal class. Since the app is destroyed so is your internal receiver class which is part of a destroyed class.
I am basing this assumption because your manifest has
android:name=".adapters.VideoListAdapter$VideoDownloadedReceiver".
Separate the 'BroadcastReceiver' into its own class and it should work as expected.
As for working on some phones, I would check the other apps running and what kind of app suppression apps you may have running in the background which may be contributing to force closing your app.
you should have something like this. i looked at code and you have nested receeiver but you are not holding the service on.
Calling function to start the action
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);
}
}
in separate file here is the service
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();
}
}
In separate file, here is the receiver
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();
}
}

Related

Android- SMS_RECEIVED + broadcast in a foreground service crash

I have a service that is running in the foreground, inside this service i have a broadcast receiver, that listen to the SMS_RECEIVED action.
When the user is inside the application (both the application and the service are in the foreground) everything works well, and i am receiving the intent.
But when the user exists the application (only the service with the broadcast is in the foreground), the service stops when sms is received.
When the service is stopped no error reports are found anywhere (not in the logcat and no crash dialog pops up).
My service with the broadcast:
public class myService extends IntentService {
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(myService.this, intent.getAction(), Toast.LENGTH_SHORT).show();
}
};
#Override
public void onCreate() {
super.onCreate();
IntentFilter i= new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
i.setPriority(999);
registerReceiver(mReceiver, receiverFilter);
startForeground(...);
}
public void onDestroy() {
super.onDestroy();
stopForeground(true);
}
}
And i also have the following permission in my manifest:
<uses-permission android:name="android.permission.RECEIVE_SMS" />
myService is declared like this in the manifest:
<service
android:name=".Services.myService"
android:enabled="true"
android:exported="false" />
I had the same problem and I solved it removing <uses-permission android:name="android.permission.RECEIVE_SMS" />. But removing this permission I can't detect incoming SMS, so I created a class like these Catching Outgoing SMS using ContentObserver and used MESSAGE_TYPE_RECEIVED = 1 instead MESSAGE_TYPE_SENT = 2. You need to add this permission <uses-permission android:name="android.permission.READ_SMS" />.

AlarmManager does not work when screen is locked

I am having some difficulties to implement a service that should run periodically.
The app works fine when the screen is on, and even if the app is removed from the recent tasks. But when the device is locked, the app stops and even when the screen turns on again the service does not return.
I had implemented a WakefulBroadcastReceiver with an action to SCREEN_ON, but it works only when the application is alive, it does not work when there is only a service running.
The Service and the WakefulBroadcastReceiver are declared like this in my AndroidManifest:
<service android:name=".FeedService" />
<receiver android:name=".AutoStart">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.SCREEN_ON" />
</intent-filter>
</receiver>
I also added the following permissions to my AndroidManifest file:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
This is my WakefulBroadcastReceiver:
public class AutoStart extends WakefulBroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("Feed", "AutoStart.onReceive");
context.startService(new Intent(context, FeedService.class));
}
}
And this is my Service:
public class FeedService extends Service {
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("Feed", "FeedService.onStartCommand");
stopSelf();
return START_STICKY;
}
#Override
public IBinder onBind(Intent intent) {
Log.d("Feed", "FeedService.onBind");
return null;
}
#Override
public void onDestroy() {
// I want to restart this service again in 5 seconds
AlarmManager alarm = (AlarmManager)getSystemService(ALARM_SERVICE);
alarm.set(
alarm.RTC_WAKEUP,
System.currentTimeMillis() + (5000),
PendingIntent.getService(this, 0, new Intent(this, FeedService.class), 0)
);
}
}
In my MainActivity I start the service with:
AlarmManager alarm = (AlarmManager)getSystemService(ALARM_SERVICE);
alarm.set(
alarm.RTC_WAKEUP,
System.currentTimeMillis() + (5000),
PendingIntent.getService(this, 0, new Intent(this, FeedService.class), 0)
);
It's critical to my application keep this service running and I don't understand why it is not happening.
Could someone explain me how can I solve this?
I found an answer to my problem.
For some reason, even if I had requested WAKE_LOCK in the manifest file and registered my BroadcastReceiver in the Activity, my app does not have a permission to continues alive after the screen lock. I found an option on my device where it can be specified in settings>protected apps. So I just checked this option e now my service is working fine. I do not know if this option is checked automaticaly when the app is installed from Google Play (I hope so), but this option wasn't checked in my case.

Broadcast receiver is not getting invoked

I have used a Broadcast Receiver which is to be invoked when it finds internet Connection on the android device, to check if the Broadcast Receiver is running or not I have tried printing it in the logcat. I have no message being displayed.
I am invoking a service in my broadcast receiver which is also not getting invoked.
I have seen similar questions on stack overflow but still I am unable to solve the issue.
I am stuck with this since many days. Can anybody help me solve this problem?
This is my Broadcast receiver:
public final class ConnectivityReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Log.e("BRRRRRRRRRR","works!!!! In Broadcast Receiver...!!!");
final AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
final PendingIntent wakeupIntent = PendingIntent.getService(context, 0,
new Intent(context, LocationUpdate.class), PendingIntent.FLAG_UPDATE_CURRENT);
final boolean hasNetwork = !intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
if (hasNetwork)
{
// start service now for doing once
context.startService(new Intent(context, LocationUpdate.class));
// schedule service for every 15 minutes
alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + AlarmManager.INTERVAL_FIFTEEN_MINUTES,
AlarmManager.INTERVAL_FIFTEEN_MINUTES, wakeupIntent);
}
else
{
alarmManager.cancel(wakeupIntent);
}
}
}
Manifest.xml
<receiver android:name="info.androidhive.loginandregistration.ConnectivityReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
</intent-filter>
</receiver>
Since you are checking connectivity changes, i suggest you can first check if your app has permissions to connect to the internet.
The permission for the app to access the internet would be -
<uses-permission android:name="android.permission.INTERNET" />
Your receiver looks fine. Please, check if your package name is correct. Registering receiver in AndroidManifest.xml should me sufficient, but if it doesn't work for some reason, you can try different solution.
Try to register your receiver programmatically in the following way e.g. in onResume() method in your activity:
#Override
protected void onResume() {
super.onResume();
ConnectivityReceiver receiver = new ConnectivityReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
registerReceiver(receiver, filter);
}
You should also unregister your receiver in onPause() method in the activity:
#Override
protected void onPause() {
super.onPause();
unregisterReceiver(receiver);
}
If it won't help, you can add new permissions to AndroidManifest.xml file inside the tag.
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

how to use broadcast receiver on receive

i have a project and when i run the application,one service should be active and when the device is turned on,my android service should be active.
The project runs in emulator successfully but in my phone when i turn on the device it doesn't works!
my broadcast receiver :
public class BroadcastReceiverOnTurnedOn extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent startServiceIntent = new Intent(context, MyService.class);
context.startService(startServiceIntent);
}
}
i added :
<receiver android:name="com.dariran.BroadcastReceiverOnTurnedOn">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
to appliation tag on Manifest.xml and
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
You are registering a BroadcastReceiver not a Service. Services will generally run in the background all of the time (if memory and resources permit).
Your broadcast receiver activates only when the device is rebooted - and your emulator "boots" every time you start it up. Your phone does not "boot" when the app is installed.
You should either register for events like "app installed" or else actually implement a service, like here:
http://www.vogella.com/tutorials/AndroidServices/article.html
You need to make sure that once you install the app on device it should be started once by clicking app icon then only boot receiver will receive boot event on subsequent reboots calls.
i undrestand that if i install app in internal storage in works successfully even device rebooted but when i install the app on external storage and then reboot device it not work
i have a project that when run application one service be active and when device be turn on my android service be active. the project run in device's internal memory successfully but when i install it on external memory and reboot my device again don't work!
for first i call my service in first acitivity and it works, but when i reboot my device it doesn't work !
my activity :
public class FirstClass extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.first);
final Handler handler = new Handler();
handler.postDelayed(new Runnable()
{
public void run()
{
startService(new Intent(getApplicationContext(), MyService.class));
startActivity(new Intent(FirstClass.this, MainActivity.class));
finish();
}
},5000);
}
/////////////////////////////////////////////////
}
my broadcast receiver :
public class BroadcastReceiverOnTurnedOn extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent startServiceIntent = new Intent(context, MyService.class);
context.startService(startServiceIntent);
}
}
i added :
<service
android:name="com.dariran.MyService"
android:enabled="true"
android:exported="true" >
</service>
<receiver android:name="com.dariran.BroadcastReceiverOnTurnedOn"
android:enabled="true">
<intent-filter android:priority="1">
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE" />
</intent-filter>
</receiver>to appliation tag on Manifest.xml and
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
i added this code to my service class to put a filter to recognize the external storage but don't work again :(
#Override
public void onStart(Intent intent, int startId) {
try {
IntentFilter filter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
BroadcastReceiver mReceiver = new BroadcastReceiverOnTurnedOn();
registerReceiver(mReceiver, filter);
} catch (Exception e) {
}
}

Can i start an android service without activity or GUI?

My target Android is 4.1.2. I created an simple android service which will show Toast on boot. But this application should not have any GUI. I was success running this service only from an activity which show GUI on start.
public class MyServices extends Service {
private MediaRecorder recorder = null;
#Override
public IBinder onBind(Intent intent) {
return null;
}
public int onStartCommand(Intent intent, int flags, int StartId)
{
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
}
}
You can start this service from RebootReceiver but As of Android 3.0 the user needs to have started the application at least once before your application can receive android.intent.action.BOOT_COMPLETED events.
Reboot Receiver -> Android BroadcastReceiver on startup - keep running when Activity is in Background
First you have to create a receiver:
public class BootCompletedReceiver extends BroadcastReceiver {
final static String TAG = "BootCompletedReceiver";
#Override
public void onReceive(Context context, Intent arg1) {
Log.w(TAG, "starting service...");
context.startService(new Intent(context, MyServices.class));
}
}
Then add permission to your AndroidManifest.xml
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
and register intent receiver:
<receiver android:name=".BootCompletedReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
After this is done, your application (Application class) will run along with services, but no Activities, don't put your application on SD card (APP2SD or something like that), because it has to reside in the main memory to be available right after the boot is completed.

Categories

Resources