Android App with Broadcast receiver after reboot as main - android

Hello i would to develop a simple app without Main Activity as launcher.
I want to register a broadcast receiver which starts after reboot of device and inside OnReceive callback starts an Activity
Here my manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="it.examples"
android:versionCode="1"
android:versionName="1.0"
>
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="18" />
<application android:label="#string/app_name" android:icon="#drawable/ic_launcher">
<receiver android:name=".AfterRebootBR" android:exported="false"
android:label="Boot Notification Receiver" android:enabled="true"
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>
<activity android:name=".MainActivity"
android:label="#string/app_name">
</activity>
</application>
</manifest>
And here my Broadcast receiver
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package it.examples;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class AfterRebootBR extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("AfterRebootBR","***************** ON RECEIVE *********************");
Log.e("AfterRebootBR","***************** ON RECEIVE *********************");
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
And finally the MainActivity
package it.examples;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
What is wrong in my code?
Thanks in advance
Francesco

my code is working..here is it...
in manifest
<receiver
android:name="com.calender.calenderevent.Reboot_Reciever"
android:enabled="true"
android:exported="true"
android:label="BootReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" >
</action>
</intent-filter>
</receiver>

I cant see anything wrong with your code, however i have something worth to try.
Move the permission out of the application tag :
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
If its not working, simplify the receiver :
<receiver android:name=".AfterRebootBR">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Still not working? Try to add some delay, as mentioned here :
Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
//run your service
}
}, 10000);
Quoted from the link above :
While, I suggestion to delay several seconds, e.g., 10 seconds, before
running the (1) line, which is more stable for different phones and
services.
For example, in my case, my service is going to write sd card. If you
start your service immediately, some phones may fail because the sd
card is not ready.

Starting with android 3.1 you cannot have a broadcast receiver to get spawned by the application service manager if it has no context in active state (aka. at least an activity or service that is keeping the process in an "active state")
Excerpt from specification
Note that the system adds FLAG_EXCLUDE_STOPPED_PACKAGES to all broadcast intents.
It does this to prevent broadcasts from background services from inadvertently or
unnecessarily launching components of stoppped applications. A background service or
application can override this behavior by adding the FLAG_INCLUDE_STOPPED_PACKAGES
flag to broadcast intents that should be allowed to activate stopped applications.
Applications are in a stopped state when they are first installed but are not yet
launched and when they are manually stopped by the user (in Manage Applications).
You need to somehow start your application, and then send it in a dormant state (but registered in the app manager). You can use a service for this.

It is strongly NOT recommended to start Activity from BroadcastReciever:
https://developer.android.com/training/run-background-service/report-status.html#ReceiveStatus
Never start an Activity in response to an incoming broadcast Intent.
In my case PackageManager.DONT_KILL_APP helped:
https://developer.android.com/training/scheduling/alarms.html#boot
ComponentName receiver = new ComponentName(context, SampleBootReceiver.class);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
I've experimented with MIUI 8 firmware real device Xiaomi Redmi Note 3.
My findings are:
you have to add app to autorun to enable it be fired by broadcast. I've checked it with such serious apps as Viber, WhatsApp.
I've compared with manifest settings (without enabling reciever programmatically):
<receiver
android:name=".activities.broadcastrecievers.CallReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>

Related

Can't start service on boot after Android 7

I want to start a service on boot.
This code only works on Android versions that came before 7.
What should I change to make it work on newer versions? Should I use JobScheduler or maybe WorkManager instead?
For what I know Timer, Handler and AlarmManager are deprecated or have different purposes.
I suspect that after Android 7 they removed the option of starting a service on boot in order to save battery life and avoid slowing down the phone. Can you confirm that?
This is my broadcast receiver
package com.kev.boot21;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class BroadcastReceiverOnBootComplete extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.i("tag","xyz broadcast 1");
if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) {
Intent serviceIntent = new Intent(context, AndroidServiceStartOnBoot.class);
context.startService(serviceIntent);
}
}
}
This is my service
package com.kev.boot21;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class AndroidServiceStartOnBoot extends Service {
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
Log.i("tag","xyz service");
}
}
This is my manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.kev.boot21">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<receiver
android:name="com.kev.boot21.BroadcastReceiverOnBootComplete"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name="com.kev.boot21.AndroidServiceStartOnBoot"></service>
<activity
android:name="com.kev.boot21.MainActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
I found that WorkManager is the best way to handle this problem.
I managed to run a PeriodicWorkRequest after reboot on Android 7, 8 and 9 using the example code from the official documentation:
https://developer.android.com/topic/libraries/architecture/workmanager/basics
Android 10 behaves in a different way. After reboot the PeriodicWorkRequest works only after opening the app. I guess it's for performance/security reasons
You need to register your receiver in your code by calling Context.registerReceiver(), as ACTION_BOOT_COMPLETED is no longer sent to receivers registered through AndroidManifest.xml

How to pass an intent to my App without drawing the Activity?

I'm implementing a simple App that can receive 'share's from the YouTube App and simply sends it to a server.
I'm able to pass the Intent to a ShareActivity I've implemented, but every time I share a video from YouTube, it opens the ShareActivity.
Since the user doesn't need to interact with the App while sharing, I'd like to remove this phase entirely. That is, I'd like to receive the Intent from Youtube and process it in the background without any hassle to the user.
I'm thinking of creating an IntentService that can handle such incoming Intents, but I'm not sure how to proceed. (Is this the right way to achieve this? If so, from where should I start the IntentService?, Will it be able to receive Intents if the user kills my app?)
I can't seem to find any resources on the Internet regarding this. Any help is appreciated.
Edit: Okay, This is a minimal, not-working example. I hope this can help you guys guide me better. Cause I'm stuck.
Android Manifest File
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.blumonkey.versatyl">
<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>
<service
android:name=".ShareService"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:host="www.youtube.com" android:mimeType="text/*" />
</intent-filter>
</service>
</application>
MainActivity.java
package com.blumonkey.versatyl;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent msgIntent = new Intent(this, ShareService.class);
startService(msgIntent);
}
}
ShareService.java
package com.blumonkey.versatyl;
import android.app.IntentService;
import android.content.Intent;
import android.util.Log;
/**
* An {#link IntentService} subclass for handling asynchronous task requests in
* a service on a separate handler thread.
* <p>
* TODO: Customize class - update intent actions and extra parameters.
*/
public class ShareService extends IntentService {
public ShareService() {
super("ShareService");
}
#Override
protected void onHandleIntent(Intent intent) {
Log.d("Msg:", "Got Intent!");
}
}
The only message that I see in the Log is when the MainActivity sends the Intent to the service to start it.
IntentService would be fine, or even a regular Service if you will need to use resources for long period of time - you just need to add an IntentFilter with the proper filter options to the service in the manifest.
The Android docs explain it for both Activities and Services:
https://developer.android.com/guide/components/intents-filters.html
Sometimes an Intent is designed to be handled by an Activity so just be cautious about that.
And this is the correct way to "wake up" your app if a user "kills" it. An Intent that your app is designed to handle will receive it. However, sometimes users can set preferred apps to handled certain Intents, in which case you do not have control over what happens.

Qt android Can't listen to os intents ex.RECEIVE_BOOT_COMPLETED

I have an android service that runs when i open my app, now I want my android service to run at boot time. I tried the bellow code, but the service is not running automatically when i reboot my device. I cannot see it running as a service on my phone! Is there something wrong in my code?
I added these permissions to the manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.RECEIVE_HEADSET_PLUG"/>
Here's my receiver in the manifest:
<receiver android:name="org.qtproject.example.MyBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.RECEIVE_BOOT_COMPLETED"/>
<action android:name="android.intent.action.RECEIVE_HEADSET_PLUG"/>
</intent-filter>
</receiver>
And here's MyBroadcastReceiver.java:
import android.os.Bundle;
import org.qtproject.qt5.android.bindings.QtActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent startServiceIntent = new Intent(context, org.qtproject.example.MyCustomAppService.class);
context.startService(startServiceIntent);
}
}
replace
<action android:name="android.intent.action.RECEIVE_BOOT_COMPLETED"/>
with
<action android:name="android.intent.action.BOOT_COMPLETED"/>
in the manifest
ok so I had 2 problems in my code:
1)Thanks to #kajay, i had to change my action line as he described,
to be:
<action android:name="android.intent.action.BOOT_COMPLETED"/> in the manifest.
2) I was missing defining the package in the MyBroadcastReceiver.java. So, the class couldn't find the startServiceIntent. Of course qt doesn't give any errors or warnings with many java problems.
So, in my case i had to add this to the MyBroadcastReceiver.java :
package org.qtproject.example;
I had to do both of the above steps to fix my problem!
P.S Sometimes the service takes around 45 secs or more to start after booting!

BroadcastReceiver is not starting Service in Android [duplicate]

In my Android application I want to run a Service without opening/running my application. For that I have extended BroadcastReciever class. But this BroadcastReceiver class is not being called from AndroidManifest.xml on BOOT_COMPLETE. So please tell what is the problem in my code? Or is there any other way to run a Service without opening my application? I have checked the control flow of my code and whole the code is working perfectly, the problem is that BroadcastReceiver is not being called.
Part of AndroidManifest.xml file:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<service android:name=".HelloService"
android:exported="false"/>
<receiver android:name=".MyBroadcastreceiver" android:enabled="true" android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
MyBroadcastreceiver.java class
package com.example.abc.project1;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyBroadcastreceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
/*this is not being called*/
Intent startServiceIntent = new Intent(context, HelloService.class);
context.startService(startServiceIntent);
}
}
Remove android:exported="false" from the <receiver>. That says you do not want anyone (other than yourself) sending a broadcast to this receiver. As a result, your receiver will be ignored by the system.
Beyond that, you also need an activity and to have run that activity before trying to reboot the device. You may already have that, but I thought that I would mention it for completeness.

BroadcastReceiver dont catch the phone state

I am trying to start a new app, but i need to know about a change in the phone state...
for some reason (i am new to this) i cant catch the broadcast of the change.
thats my code:
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) {
Log.d("TAG","yyyyyyyyyyyy");
}
}
and thats my manifest:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver
android:name=".MyReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
</application>
as you can see... very simple.
but i cant see the Log in the onReceive on my LogCat.
any one knows why?
Thanks!
Due to this and this, your BroadcastReceiver won't receive anything. The solution is to design a simple activity for your app that starts at least once after installation, before it starts receiving.
In order for a Manifest based Broadcast Receiver to get registered, you need to run your app by opening it from the app drawer. If you don't do that, then the receiver will not be called by Android. So basically, you need at least one Activity that can start - it does not have to do anything, it just has to be started at least once. Did you do that?
This is true after Android 3.0+

Categories

Resources