Service does not start when the phone is started - android

I'm pretty new to android programming. I'm trying to start a service when the phone is started but it does not work. I've already seen other question done by other users but no one worked till now. This is my broadcast receiver.
public class StartBoot extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
Intent intent1 = new Intent(context,MyService.class);
context.startService(intent1);
}
}
}
and this is my manifest
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:allowBackup="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=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".StartBoot">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
<service
android:name=".MyService"
android:enabled="true"
android:exported="true"></service>
</application>
In the log i read this
W/BroadcastQueue: Permission Denial: receiving Intent { act=android.intent.action.BOOT_COMPLETED flg=0x9000010 (has extras) } to com.google.android.apps.docs/.app.NotificationChannelReceiver requires android.permission.RECEIVE_BOOT_COMPLETED due to sender null (uid 1000)
Thank you in advance for the answers

Remove
<category android:name="android.intent.category.DEFAULT"/>
from the <intent-filter> inside <receiver> tag. This category is only needed for <intent-filter> inside of <activity> declaration. For a <receiver> you only need the ACTION.
The error message you posted has nothing to do with your application, it is complaining about another application.

Related

How to create an application that starts the service after boot up sequences are finished?

I want to create an app that starts the service after rebooted, but I do not want to show the UIā€“just like the service run in the background silently. I can create it,but after rebooted the application crashes. Because, MainActivity had not launched and I don't want to launch any activity. How do I solve this problem?
My manifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.galleryapp">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.CAMERA" />
<application
android:name=".App"
android:allowBackup="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=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".CameraEventReceiver"
android:enabled="false">
<intent-filter>
<action android:name="com.android.camera.NEW_PICTURE" />
</intent-filter>
</receiver>
<receiver android:name="com.galleryapp.RebootDeviceReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service
android:name=".BackgroundService"
android:exported="true" />
</application>
And my receiver class:
public class RebootDeviceReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent serviceIntent = new Intent(context, BackgroundService.class);
context.startService(serviceIntent);
}
}
You need to write a BroadcastReceiver and register it in your Manifest file.
<receiver android:name="your.package.BootCompletedReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
From your BroadcastReceiver you can then start a service. For example, in my code I'm using a JobIntentService.
public class BootCompletedReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent msgIntent = new Intent(context, MyJobIntentService.class);
msgIntent.setAction(MyJobIntentService.ACTION_RESCHEDULE);
MyJobIntentService.enqueueWork(context, msgIntent);
}
}
If you want your service to run for a longer amount of time, you need to run it as a foreground service, at least for newer Android versions.

Battery broadcast receiver declared in manifest doesn't work

I want to receive broadcast BATTERY_LOW, so I declared broadcast receiver in manifest (in order not to rely on current activity. I want my application receive this broadcast even when it's not running. Here's what I did.
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<receiver android:name=".BatteryLevelReceiver">
<intent-filter>
<action android:name="android.intent.action.BATTERY_LOW"/>
<action android:name="android.intent.action.BATTERY_OKAY"/>
<action android:name="android.intent.action.POWER_CONNECTED"/>
<action android:name="android.intent.action.POWER_DISCONNECTED"/>A
</intent-filter>
</receiver>
<activity
android:name=".MainActivity"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
And BatteryLevelReceiver itself (package com.wayruha.serviceguard.onduty) :
public class BatteryLevelReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.w(this.getClass().getName(),intent.getAction());
Log.w(this.getClass().getName(),"Low level:"+intent.getIntExtra(BatteryManager.EXTRA_LEVEL,-1));
}
}
I cant receive any of declared events!There are some similar questions here, but none of them helps me. Im using telnet to set capacity or 'ac on/off' for an emulator.
Try to add permission in your manifest file:
<uses-permission android:name="android.permission.BATTERY_STATS"/>

Android - BroadcastReceiver Not Triggering for BOOT_COMPLETED

I am working with a Samsung Galaxy Grand Prime that uses Android 5.0.2. What I want to do is simple, I want my application to boot up when the phone restarts. I am aware that to activate broadcasters I must run the application manually first after installing it. I do this and it still doesn't work. Here's my broadcaster code:
public class onStartupReceiver extends BroadcastReceiver {
public onStartupReceiver() {
}
#Override
public void onReceive(Context context, Intent intent) {
Log.e("PLEASE", "System boot notification received.");
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Log.e("PLEASE", "System boot notification received.");
Intent i = new Intent(context,TestGPSBackgroundActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
And here's my manifest relevant code:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:name="android.support.multidex.MultiDexApplication"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme"
android:installLocation="internalOnly">
<activity
android:name=".TestGPSBackgroundActivity"
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>
<receiver
android:name="com.example.jorge.gpsevixel.onStartupReceiver"
android:enabled="true"
android:exported="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >>
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="android.intent.action.REBOOT"/>
<action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
I've been doing a lot of research that suggests removing the line android:permission="android.permission.RECEIVE_BOOT_COMPLETED" from the receiver and such, but no luck.
I've read things like App doesn't auto-start an app when booting the device in Android and a lot of similar questions/answers but none has helped me. Hope Anyone can help me I"m getting desperate.

Broadcastreceiver not working after boot

I have created an apps that will receive notifications from firebase thus hoping to start the app service after user boot their phone so that they do not need to manually start the apps again. However, the broadcast receiver seems to just not working.
AndroidManifest.XML
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.simplifiedcoding.firebasecloudmessaging">
<!-- Adding Internet Permission -->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!--
Defining Services
-->
<service
android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service
android:name=".MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
<service android:name=".NotificationService"/>
<receiver android:name=".Broadcast" android:exported="true" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>
</manifest>
Broadcast.java (BroadcastReceiver)
public class Broadcast extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent){
Intent service = new Intent(context, NotificationService.class);
context.startService(service);
Toast.makeText(context, "Broadcast started", Toast.LENGTH_LONG).show();
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
I have checked the followings
Permission RECEIVE_BOOT_COMPLETED declared and not within tag
receiver tag is being written correctly
Am I doing wrong or still missing something else? Do I have to call it at mainAcitivity which I doubt I should ? Any guidance are much appreciated.
Got the solution and it was a very dumb reason.
I'm using Oppo's phone to test and Oppo has its own Security Manager App where you have to manually allow specific apps to be start up automatically after boot. That's all.
I suspect most android phones has this feature as well thus bear in mind to check whether there is such app and if it does then remind the user to allow the apps in the Security Manager App before they start rebooting their phone and not able to use any service's your App intended to provide.
Hope this helps!
Try this, it worked for me.
<receiver android:enabled="true" android:name=".Broadcast"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>

How to intercept incoming sms with my custom sms app?

I've almost made my sms app, only one bug still remains. When phone receives incoming sms it somehow goes to my app and to default one. I thought that problem was in action of intent, but changing it does nothing. The question is - how to intercept incoming sms and forbid default app to do the same?
AndroidManifest:
<manifest package="lexzcq.com.smska"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission
android:name="android.permission.SET_PREFERRED_APPLICATIONS"
tools:ignore="ProtectedPermissions"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<application
android:allowBackup="true"
android:icon="#drawable/logo"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:launchMode="singleTask"
android:theme="#style/Theme.AppCompat.Light.NoActionBar">
<intent-filter android:priority="999">
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
<category android:name="android.intent.category.APP_MESSAGING"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
<receiver
android:name=".SmsReceiver"
android:permission="android.permission.BROADCAST_SMS"
>
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
<category android:name="android.intent.category.APP_MESSAGING"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
<activity android:name=".ChatWindow">
<intent-filter>
<action android:name="com.lexzcq.com.lexzcq.sms_receive_intent"/>
</intent-filter>
</activity>
<application android:name=".MyApp">
</application>
</application>
</manifest>
SmsReceiver.class
public class SmsReceiver extends BroadcastReceiver {
#Override
public void onReceive (Context context, Intent intent) {
*//here I handle sms from pdus*
Intent broadcastIntent = new Intent ()
.putExtra ("address", address)
.putExtra ("body", body)
.setAction ("com.lexzcq.sms_receive_intent");
*//here I'm writing sms to database*
context.sendBroadcast (broadcastIntent);
abortBroadcast ();
}
}
}
If needed - I can provide broadcast receivers from MainActivity.class
P.S. Most weird part - that when I'm sending sms it somehow saves to default app but I didn't broadcast it with SMS_RECEIVED_ACTION.

Categories

Resources