android receiver stops after clean ram - android

I have a gcm receiver, but it is not working before restarting phone and after clean memory. What should i change to guarantee receiver is always working?
<activity
android:name="com.example.diyet.MainActivity"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.plugin.gcm.PushHandlerActivity"/>
<receiver android:name="com.plugin.gcm.CordovaGCMBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.plugin.gcm" />
</intent-filter>
</receiver>
<service android:name="com.plugin.gcm.GCMIntentService" />,
thanks in advance

if u want to start your receiver manually then use
registerReceiver(receiver) inside your activity

Sorry I don't know anything about GCM but if I get you right you have one of the following possibilities
If your GCMIntentService is custom, you can make this Service STICKY So the OS restarts it after a Crash. To do this you have to return return Service.START_STICKY; in your onStartCommand Method.
Also you can Register for the android.intent.action.BOOT_COMPLETED to get your Service started right after device is Booted.
If you want to prevent the Service to Crash, when the App Crashes you have to put it in another Thread. This can be achieved by Setting an custom Process Name in the Manifest
android:process="com.gigatronik.presenter.serialport.SerialPortListenerService"/
I will edit this answer, if you can give me more information on your Problem, since I never used GCM

Related

On Boot Broadcast Receiver not working in miui of Xiaomi (Poco x3)

I know many similar questions have been asked before, but I couldn't make it work no matter what solution I tried.
I have a broadcast receiver code like the following.
class OnBootBroadcast : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
logD("onReceive() started -> intent action: [${intent?.action}]")
// this is only to test if on boot broadcast is working
context?.let {
val i = Intent()
i.setClass(it, MainActivity::class.java)
i.flags = Intent.FLAG_ACTIVITY_NEW_TASK;
it.startActivity(i);
}
// tried to test by adding notification as well, didn't show
// do stuff here
}
}
My manifest file is like
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:name=".CustomApplication"
android:allowBackup="true"
android:fullBackupContent="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".view.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".broadcast.NotificationBroadcast" />
<receiver
android:name=".broadcast.OnBootBroadcast"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
<provider
android:name="androidx.work.impl.WorkManagerInitializer"
android:authorities="${applicationId}.workmanager-init"
tools:node="remove" />
</application>
I know Chinese custom ROMs like MIUI kill apps' background services.
To handle that, I tried the following things.
Turn on "Autostart" in Security > Manage apps
This fixed issue of work manager background service not working when app is closed by swiping. But didn't fix the broadcast issue.
Set "No restrictions" in Settings > Battery & Performance > App battery saver
I am trying to use the on boot broadcast to re-add some alarm managers for exact timed notifications.
If there is some alternative which can achieve this, that info would be appreciated too.
Thanks in advance :)
I had the exact same problem with my Mi phone. I tried many suggested solutions but it didn't work. So I got a bit carried away and added every possible BOOT_COMPLETE trigger to my intent-filter and it worked.
<receiver android:name=".receiver.BootReceiver"
android:enabled="true"
android:exported="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.ACTION_BOOT_COMPLETED" />
<action android:name="android.intent.action.REBOOT" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="com.htc.intent.action.QUICKBOOT_POWERON" />
<action android:name="android.intent.action.ACTION_SHUTDOWN" />
</intent-filter>
</receiver>
In the end, I didn't need to change anything in manifest file. Only android.intent.action.BOOT_COMPLETED was fine as the rest of the implicit broadcasts are not in exception list.
The main problem was that MIUI is taking some minutes to start the broadcast, which I was not noticing in the log. And when it did start, since the test code was starting an activity from background process, MIUI was killing it (according to log, starting a foreground UI is not enabled for the background process started by MIUI for boot broadcast). So after I removed the activity starting code, further logs started showing too.

BOOT_COMPLETED Receiver works on hard power down/power up, but not simple restart

After a weird issue, I finally got my BootReceiver receiving the BOOT_COMPLETED signal from Android, kind of. It only works for hard power off/power on boots, it doesn't detect restarts from devices like HTC. I have implemented QUICKBOOT intent-filters in my Receivers, but still no luck.
My manifest contains:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
....
<receiver android:name="com.smashingboxes.speed.BootReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.ACTION_BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
<receiver android:name="com.smashingboxes.speed.ShutdownReceiver" >
<intent-filter>
<action android:name="android.intent.action.ACTION_SHUTDOWN" />
<action android:name="android.intent.action.QUICKBOOT_POWEROFF" />
</intent-filter>
</receiver>
I do not detect Shutdown or Boot if I do a HTC restart. Any ideas why it only seems to be working on hard shutdown/boots?
AFAIK, those aren't the right "quickboot" actions. There are no actions with the names you have specified in the Android source code.
Try com.htc.intent.action.QUICKBOOT_POWERON and com.htc.intent.action.QUICKBOOT_POWEROFF.

Registering a Phonestate receiver after rebooting

I have implemented a receiver like this (in the manifest file)
<receiver android:name="com.phonelight.realparrot.RecorderBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE">
</action>
</intent-filter>
</receiver>
If the state of the phone changes, the recorder broadcast receiver is invoked.
Everything is fine. However, If I reboot the device, the receiver is never invoked until I run my application.
I need to register (not invoking) this receiver after booting.
Many thank,
[Edit]
I solved the problem by adding the following receiver to the Manifest file
<receiver android:name="com.phonelight.realparrotpro.RecorderBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
I did not register the RecorderBroadcastReceiver in the java code though.
I only added the above receiver. It means invoking anything from an app will register all the receivers written in the Manifest file.
You need to create a receiver for onBootComplete and then register your receiver there. This way your receiver will get registered even after reboot.
<receiver android:name="App_Receiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>

android) can i set a default activity that runs right after installing

in my app, there is two activity and i want to make activity1 to the starting activity after installation.
But now the RUN button (is showed right after packgae installing) is disabled.
below is the manifest file. thanks.
<activity ...1>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity ...2>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
</intent-filter>
</activity>
android) can i set a default activity that runs right after installing
No activity "runs right after installing". The user has to launch it from the launcher.
below is the manifest file
No, it is not. That is not even valid XML.
Also, note that your third <intent-filter> is invalid. Not only are you missing any category (you need at least DEFAULT for activities), but ACTION_POWER_CONNECTED and ACTION_POWER_DISCONNECTED are not activity actions.
I am going to take a guess that you really mean to ask: "I have two activities, both described as ACTION_MAIN/CATEGORY_LAUNCHER, and now the Run button does not work -- what can I do?" The answer would be "either remove the ACTION_MAIN/CATEGORY_LAUNCHER <intent-filter> from one of them, or mark one of the two as disabled (android:enabled="false") and enable it later on using PackageManager."
I think the problem is the second:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
You shouldn't have 2 activities flagged as the MAIN and LAUNCHER activity.
Try removing it within activity2.
Check out: http://developer.android.com/reference/android/content/Intent.html the intentfilter s are discussed.

Can I start a service without activity or receiver?

I want to start a service in an APK.
I tried to use as following:
<application android:icon="#drawable/icon" android:label="#string/app_name">
<service android:name =".TestServcie">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</service>
</application>
Any ideas?
Thanks
You can write a BroadcastReceiver and run the Service after receiving the Intent. For example after device boot-up or other Intent that you need.
<receiver android:name=".StartupReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.HOME"/>
</intent-filter>
</receiver>
No you can't.
Create a simple Activity which starts the service and simply provides some feedback to the user (to tell them the service has started for example) and set that Activity with the MAIN/LAUNCHER intent.

Categories

Resources