Service not starting at reboot - android

I'm trying start a service at the time of booting the device. I have searched many all over and followed the steps and it is still not working out. Once the mobile reboots it shows in the beginning that "application has stopped unexpectedly". I know it is from the receiver file, but couldn't figure out where particularly the problem is.
Receiver.java
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class Receiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent arg1) {
// TODO Auto-generated method stub
if(arg1.getAction().equals(Intent.ACTION_BOOT_COMPLETED))
{
Intent myIntent = new Intent();
myIntent.setAction("com.androidhive.jsonparsing.UpdateService");
context.startService(myIntent);
}
}
}
AndroidManifest
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name=".AndroidJSONParsingActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Single List Item View -->
<activity
android:label="Single Menu Item"
android:name=".SingleMenuItemActivity" >
</activity>
<service android:name=".UpdateService">
<intent-filter>
<action android:name="com.androidhive.jsonparsing.UpdateService"/>
</intent-filter>
</service>
<receiver android:name=".MyReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>
Is there anything I need to add so that the service starts perfectly.

Please check you android manifest it should be like this :
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name=".AndroidJSONParsingActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Single List Item View -->
<activity
android:label="Single Menu Item"
android:name=".SingleMenuItemActivity" >
</activity>
<service android:name=".UpdateService">
<intent-filter>
<action android:name="com.androidhive.jsonparsing.UpdateService"/>
</intent-filter>
</service>
<receiver android:name=".Receiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
The problem is in declaration of your receiver.
Please check the android:name tag, it should have the right class name.

declare your receiver in AndroidManifest.xml as:
<receiver android:name=".Receiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
because you have BroadcastReceiver class as Receiver.class but declaring it in AndroidManifest.xml with MyReceiver

Instead of using
Intent myIntent = new Intent();
myIntent.setAction("com.androidhive.jsonparsing.UpdateService");
context.startService(myIntent);
Try doing this instead in the receiver
Intent myIntent = new Intent(context, UpdateService.class);
context.startService(myIntent);

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.

Service does not start when the phone is started

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.

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.

Launch activity on Boot completed and another activity on Mount completion

This my Androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.abc.testapp"
android:versionCode="1"
android:versionName="Pm61" >
<uses-sdk android:minSdkVersion="15"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<supports-screens android:anyDensity="true" />
<application android:label="#string/app_name" android:debuggable="true" android:largeHeap="true">
<activity android:name="com.abc.testapp.MainClass" android:label="#string/app_name" android:theme="#android:style/Theme.Translucent" android:hardwareAccelerated="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
.
.
.
.
.
.
<activity android:name="com.abc.testapp.BootLoad"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen" >
</activity>
<activity android:name="com.abc.testapp.Rxmain" android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen" android:launchMode="singleTask">
</activity>
<receiver android:name="com.abc.testapp.MyReceiver" android:enabled="true">
<intent-filter android:priority="500">
<action android:name= "android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.MEDIA_MOUNTED"/>
<data android:scheme="file" />
</intent-filter>
</receiver>
</application>
</manifest>
This is my MyReceiver class for broadcasting
package com.abc.testapp;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
if(Intent.ACTION_MEDIA_MOUNTED.equals(action))
{
Log.d("MYReceiver","Mounting Successfull");
Intent serviceActivity = new Intent(context, Rxmain.class);
serviceActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(serviceActivity);
}
if(Intent.ACTION_BOOT_COMPLETED.equals(action))
{
Log.d("MYReceiver","Boot Successfull");
Intent serviceActivity = new Intent(context, BootLoad.class);
serviceActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(serviceActivity);
}
}
}
My device is stand alone device, only my app will come on boot.
I want when boot complete it shall launch BootLoad activity and after MEDIA_MOUNTED it should launch Rxmain Activity.
But my bootLoad activity is not coming
So I have some doubts in this:
It is working sometimes but sometimes not?
2.what is this Priority in Intent-filter?
what this data scheme does?
what I am doing is correct or not?
please suggest me
The problem is the way you've defined the intent filter for the broadcast receiver. This is your definition:
<receiver android:name="com.abc.testapp.MyReceiver" android:enabled="true">
<intent-filter android:priority="500">
<action android:name= "android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.MEDIA_MOUNTED"/>
<data android:scheme="file" />
</intent-filter>
</receiver>
You've defined a single intent filter that will be triggered if the ACTION is either BOOT_COMPLETED or MEDIA_MOUNTED. But, by specifying the <data> tag, you will only receive broadcast Intents that have data with scheme=file.
The BOOT_COMPLETED Intent doesn't have any data, so your receiver won't get it.
You need to specify 2 separate intent filters, like this:
<receiver android:name="com.abc.testapp.MyReceiver" android:enabled="true">
<intent-filter android:priority="500">
<action android:name= "android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
<intent-filter android:priority="500">
<action android:name="android.intent.action.MEDIA_MOUNTED"/>
<data android:scheme="file" />
</intent-filter>
</receiver>

Implicit Intent not being called

I am trying to use Implicit intent to launch an activity within the same application and for an activity of another application(my other application, not the native one), but couldn't succeed in any of the cases.
Here is my sample code for the first part (i.e. to launch an activity within the same application):
Inside Activity TESTActivity
Intent intent = new Intent();
intent.setAction("com.myapp.game.myimplicit_action");
startActivity(intent);
and here is my manifest file declaration for some activity say 'ImplicitActivity' with the same action:
<activity
android:name=".TESTActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ImplicitActivity">
<intent-filter>
<action android:name="com.myapp.test.myimplicit_action" />
</intent-filter>
</activity>
Both the activities TESTActivity and ImplicitActivity are in the same application under same package. Still my ImplicitActivity activity is not getting called.
I have figured out the problem. Posting the answer for the others facing the same problem.
We need to add Default Category in order to make Implicit intents work. So here is the correct manifest entry for the same activity :
<activity
android:name=".TESTActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ImplicitActivity">
<intent-filter>
<action android:name="com.myapp.test.myimplicit_action" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

Categories

Resources