How to Launch an App After Rebooting / Startup - android

I've read some of the threads here explaining about launching an app after startup such as this one but it didn't work for me.
Here is MainActivity.java
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
AfterBootReceiver.java:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class AfterBootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("Intent.ACTION_BOOT_COMPLETED")) {
Toast.makeText(context, "AfterBootReceiver - boot", Toast.LENGTH_SHORT).show();
}
}
}
Here is the manifest with the permission code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.liorle.startappafterboot">
<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="false"
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=".AfterBootReceiver">
<intent-filter>
<action android:name= "android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
My questions is: does any of you see any problem with this code? how to make the receiver receive the boot action from within the main activity?
Thanks!

You will need to register the receiver outside of your main Activity (in your Manifest), Android will not know that your BroadcastReceiver exists until your Activity is started. You will also need to create your own BroadcastReceiver implementation for this purpose.
<receiver android:name=".BootCompletedReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
You may start yourt main Activity from inside your newly created BroadcastReceiver (BootCompletedReceiver).
Your application will also need to request the RECEIVE_BOOT_COMPLETED permission.
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

Your launcher is not listening for the BOOT_COMPLETED broadcast.
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
In your apps current state - the main activity needs to be running to listen to the BOOT_COMPLETED broadcast. Your manifest needs to have something registered that tells the ecosystem to activate the activity / receiver.

Related

How to know why receiver is not calling class and onReceive not executing

Im building an android app to detect incoming calls. It use to work a year ago, but something i´ve change has mess it up.
I reduce the code to the basis to debug, but i cant still make a Toast Notification apear when phone is reciving calls.
Has it been some major change to the permissions with reading phone state? I´ve made the modification for androdi 9 adding the ReadCallLog permision, but nothing seems to change...
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.myapplication">
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_CALL_LOG" />
<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="MyPhoneReceiver" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" >
</action>
</intent-filter>
</receiver>
</application>
</manifest>
MyPhoneReceiver.java
package com.example.myapplication;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;
public class MyPhoneReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Eureka!", Toast.LENGTH_SHORT).show();
}
}
See that and this example. You need to approve the runtime permission READ_PHONE_STATE.

Restarting service after reboot by using Broadcastreceiver is not working

I want to auto-start my service after device being rebooted as given on this website Example.
Created BroadCastReceiverBootUpComplete class to restart the service(MyService.java) after reboot, but it is not starting at all.
I want MyService.java to run as soon as device get rebooted.
MainActivity.java
package com.example.shubham.servicedemo;
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);
startService(new Intent(this,MyService.class));
}
}
BroadCastReceiverOnBootComplete.java
package com.example.shubham.servicedemo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class BroadCastReceiverOnBootComplete extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)){
Intent serviceIntent = new Intent(context,MyService.class);
context.startService(serviceIntent);
}
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.shubham.servicedemo">
<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: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="com.example.shubham.servicedemo.BroadCastReceiverOnBootComplete"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<data android:scheme="package" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
<service
android:name=".MyService"
android:enabled="true"
android:exported="true"></service>
</application>
</manifest>
If you are targeting android version 26 above then you have to use startforeground service
Intent mIntentForService = new Intent(context, MyService.class);
mIntentForService.setAction(intent.getAction());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
context.startForegroundService(mIntentForService);
else
context.startService(mIntentForService);
check below link for more details
link
Note that this works, to an extent, even if your service does not actually
ever call startForeground(). You are given a window of time to get around
to calling startForeground(), "comparable to the ANR interval to do this".
If your work is longer than a millisecond but less than a few seconds,
you could skip the Notification and the startForeground() call.

android BroadcastReceiver doesn't get called

I've seen a lot of question like this around and I've tried a bunch of things without success...
I just want to log a sms received using a subclass of BroadcastReceiver like I've seen in so many tutorial...
so here is my code and config
BroadcastReceiver
package com.example.koki.mysms;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class SmsReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("MainActivity", "test");
Toast.makeText(context, "ON RECEIVE BROADCAST", Toast.LENGTH_LONG).show();
}
}
there's nothing in my MainActivity beside what you get when creating a new projet with android studio.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.koki.mysms" >
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".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>
<receiver android:name=".SmsReceiver" android:exported="true">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
the way I test it (not very familiar in android development yet), is that I have two emulator and I sent sms to one another. They both receive the sms but nothing in the logcat nor the Toast appear. what am I doing wrong?

Android broadcast is not working when the app is not running

Please don`t vote down if this is a silly question please share some light here
i want to created an app for saving call history
for that i followed
http://karanbalkar.com/2014/02/detect-incoming-call-and-call-hangup-event-in-android/
this site
but i`m not getting the event when the app is not running.Please help
i want to get the event when a incoming call is received even when the app is closed
manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sars.broadcasttest">
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<activity android:name=".Main">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".IncommingCall">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
</application>
</manifest>
class file
package com.sars.broadcasttest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
/**
* Created by athulbabu on 18/08/15.
*/
public class IncommingCall extends BroadcastReceiver {
int i=0;
#Override
public void onReceive(Context context, Intent intent) {
Log.d("IncomingCall", "Call catched test"+i++);
Toast.makeText(context, "Calll Intent Detected. test", Toast.LENGTH_LONG).show();
}
}
For security reasons you have to run app at least once after install to get the BroadcastReceiver working.
Read this section: Launch controls on stopped applications
http://developer.android.com/about/versions/android-3.1.html

BroadcastReceivers on android 4.0.3 (ICS)

I have been trying to use the BroadcastReceiver on ICS from a while now.
I have tried all kinds of solutions, but nothing worked for me.
Here is how my manifest file looks like...
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sp.android"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".XYZActivity"
android:label="#string/app_name"
android:windowSoftInputMode="adjustResize" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".ScreenOffReceiver">
<intent-filter>
<action android:name="android.intent.action.SCREEN_OFF" />
</intent-filter>
</receiver>
</application>
</manifest>
And here is my Receiver code...
package com.sp.android;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class ScreenOffReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context arg0, Intent arg1) {
Log.e("AppSearch", "Message");
}
}
The activity class has some code in it. But now this broadcast receiver never gets registered it seems. The onReceive() method does not get called. I could not find any solution. All the threads mentioned that I need to have at least one activity, but I already have it.
Thank you in advance!
You also need to start the activity (via the launcher) at least once, otherwise your app will be in a 'stopped' stated, and won't receive any broadcasts.

Categories

Resources