Does every device send the BOOT_COMPLETED? I want to start an Activity on Boot Completed.
I put the following in the Manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name=".BootFinished">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Created the following class (receiver):
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.widget.Toast;
public class BootFinished extends BroadcastReceiver {
#Override
public void onReceive(Context mContext, Intent intent) {
if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
//do something like start an activity or service
}
try {
PackageManager pm = mContext.getPackageManager();
Intent launch = pm.getLaunchIntentForPackage("com.example.afterboot");
mContext.startActivity(launch);
} catch (Exception e) {
Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_SHORT);
}
}
}
Am I missing something? Thanks!
Add full path and secondly add permission in your receiver.
<receiver android:name="com.example.BootFinished"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
As a starting point, try putting the full path of your receiver in the manifest.
The possible reasons why broadcast reciever events could fail:
Receiver not declared in AndroidManifest.xml
Declare the receiver in the Manifest-file:
Receiver in the Manifest xml is misspelled
Android-System is case sensitive. So check your spelling and path is correct in the AndroidMainfest.xml
AVD running for a long time
Reset your avd/device
4.Also if your app is in moved to sdcard.Say you have registered for android.intent.action.BOOT_COMPLETED,the boot event is triggered even before the mediascanner scans the sdcard.
and all the devices which run android sends BOOT_COMPLETE :P
Check again and try :)
all the best :)
Related
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!
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.
I am listening to deskclock alarm change intent using my broadcast receiver. When the onReceive() method in my Broadcast Receiver is called, the logs (Log.i/v()) inside the onReceive are not getting printed on Android monitor but Toasts are working just fine.
Manifest File :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dumbrella.ratemyday">
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<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>
<receiver android:process=":remote" android:name="AlarmClockReceiver">
<intent-filter>
<action android:name="com.android.deskclock.ALARM_DISMISS" />
<action android:name="com.android.deskclock.ALARM_SNOOZE" />
<action android:name="com.android.deskclock.ALARM_ALERT" />
<action android:name="com.android.deskclock.ALARM_DONE" />
</intent-filter>
</receiver>
</application>
</manifest>
Broadcast Receiver:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
import java.text.SimpleDateFormat;
public class AlarmClockReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// This log.v does not get printed on Android Monitor
Log.v("Broadcast Test", "In Broadcast Listener");
String message = "Broadcast intent detected "
+ intent.getAction();
// This toast gets displayed after the alarm is dismissed
Toast.makeText(context, message,Toast.LENGTH_LONG).show();
}
}
Maybe it's too late, but I leave it for future programmers with this problem, I had a similar problem and solved it this way.
Simply in Logcat panel, change the filtering listbox to "No Filters".
You might want to make sure that you have selected the correct device in your DDMS perspective, also ensure that you have selected the correct filtering option and log level - in your case you want to select "verbose". Also check out the selected answer and suggestions as to why Logcat is not displaying logs.
You can filter logs to show BroadcastReceiver logs in LogCat Filter Configuration section:
The complete solution steps: Logs/logcat not working in BroadcastReceiver
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>
I am trying to write a code in Android , to create a condition during booting but my condition satisfies everytime ( during booting as well as during running of the device also). I am trying to do is , to execute the condition during the booting only.
My Code :
MainActivity.java
package com.example.bootingtest;
import android.os.Bundle;
import android.widget.Toast;
import android.app.Activity;
import android.content.Intent;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Intent.ACTION_BOOT_COMPLETED!=null) {
Toast.makeText(getApplicationContext(), "Device is booting ...", Toast.LENGTH_LONG).show();
}
}
}
I have given manifest permission .
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
I want to execute this condition only during booting or device start-up but this condition satisfies every time , whenever I open the app.
Please suggest to me how I can run the condition only during the device booting or start-up.
Please help me out.
The Intent.ACTION_BOOT_COMPLETED is a constant, so it's values never changes, that's why you get always true when you activity starts.
What you have to do is declare a BroadcastReceiver on the manifest and implement it, than add a IntentFilter on your declaration to receive the broadcast.
Something like this:
AndroidManifest.xml:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
....
<receiver
android:name=".BootReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
...
BootReceiver.java:
public class BootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
//Do what you need to execute on boot here.
}
}
the class Intent has a string called ACTION_BOOT_COMPLETED that is not null (it has a value). that is what you are checking. what you mean to do is done completely different in a BroadcastReceiver.