Autostart app immediately after the boot - android

I want to start my application when phone startup
I just follow tutorial from here but it doesn't work in my device. Please see my method:
package net.londatiga.android;
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, ExampleActivity.class);
context.startService(startServiceIntent);
}
}
And this is my manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.londatiga.android"
android:versionCode="2" android:versionName="1.01">
<uses-sdk android:minSdkVersion="7"
android:targetSdkVersion="15"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<receiver android:name="net.londatiga.android.MyBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="1000">
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<activity android:name=".ExampleActivity"
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>
Where is my mistake please?

Instead of:
context.startService(startServiceIntent);
Use:
context.startActivity(startServiceIntent);

You don't have any Service, You need to open activity.
Intent startServiceIntent = new Intent(context, ExampleActivity.class);
context.startActivity(startServiceIntent);

Create a class name it as AfterBootActivity:
public class AfterBootActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}}
Now, Create a new class named as Autostart.java which extends a BroadcastReceiver:
public class Autostart extends BroadcastReceiver {
public void onReceive(Context context, Intent arg1) {
Intent intent = new Intent(context, StarterService.class);
context.startService(intent);
}}
In the Manifest file add this class as a receiver. This class will listen to the Broadcast call the Android OS sends after the boot sequence has finished i.e. after the phone started up.
Now Create a class named as StarterService.java which will extend Service:
public class StarterService extends Service {
private static final String TAG = "MyService";
public IBinder onBind(Intent intent) {
return null;
}
public void onDestroy() {
Toast.makeText(this, "Service stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
}
/**
* The below started service opens the Activity.
*/
public void onStart(Intent intent, int startid) {
Intent intents = new Intent(getBaseContext(), AfterBootActivity.class);
intents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intents);
Toast.makeText(this, "Service started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
}}
When the class Autostart receives the BOOT_COMPLETED Broadcast from Android OS it will start the StarterService which then starts the Android Activity “AfterBootActivity” i.e our main class. We can play any audio/video or anything in it.
Change your Manifest.xml as below:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="on.boot.completed"
android:installLocation="internalOnly"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name="on.boot.completed.AfterBootActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="on.boot.completed.Autostart" >
<intent-filter>
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service
android:name="on.boot.completed.StarterService"
android:enabled="true"
android:exported="true" />
</application>
</manifest>
Also Remember to install it in internal memory because if the app installed on the SD Card then autostart will not work! That’s why it’s important that we add in manifest.
android:installLocation="internalOnly"
That’s all run your app.
After it has been started turn off your phone and turn it back on and the app would start automatically after the device has booted up.

Related

Application crash to start, when using BootReceiver

I want my application to launch browser when boot my tablet but its crashing on boot time. If someone please let me know my mistake? I write nothing in my activity_main.xmle. Its showing me message "unfortunately the application crashed".
mainfest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.autostartmyapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/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=".BootReciever" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
mainActivitc class
package com.example.autostartmyapp;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://google.com/"));
startActivity(browserIntent);
}
public class BootReciever extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(context, MainActivity.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
}
}
}
separate both classes and fix mainfest like below
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.autostartmyapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/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="com.example.autostartmyapp.BootReciever"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
</manifest>
write MainActiity
MainActivity {
...}
BootReceiver class
package com.example.BootReciever;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class BootReciever extends BroadcastReceiver {
#Override
public void onReceive(Context ctx, Intent arg1) {
Intent iMainActivity = new Intent(ctx, yourMainActivityClass);
iMainStartActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(iMainActivity);
}
}
At a guess, I'd say it's probably because your manifest specifies .BootReceiver as the receiver, but based on your code the BootReceiver class is nested inside MainActivity. So you either need to move BootReceiver to a top-level class, or change your manifest to specify .MainActivity.BootReceiver as the receiver.
If that doesn't resolve it, please post your logcat so we can see what error is causing the crash.
Use this in the Manifest as receiver section:
<receiver
android:name=".BootReciever"
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>
And take care of Broadcast Receiver class that should be not inner:
package com.example.BootReciever;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class BootReciever extends BroadcastReceiver {
#Override
public void onReceive(Context ctx, Intent arg1) {
Intent iMainActivity = new Intent(ctx, MainActivity.class);
iMainStartActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(iMainActivity);
}
}
Your app starts so quick and doesn't let other important apps to start first I think you should add some delay in your app to start up. like this:
public class BootUpReceiver extends BroadcastReceiver {
#Override
public void onReceive(final Context context, Intent intent) {
// We can't wait on the main thread as it would be blocked if we wait for too long
new Thread(new Runnable() {
#Override
public void run() {
try {
// Lets wait some time before starting the service to be fair to other processes on startup
Thread.sleep(5000);
} catch (InterruptedException e) {
}
Intent myIntent = new Intent(context, MainActivity.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
}
}).start();
}

How to call an activity before screen lock is shown?

I want to start an activity even before screen lock after rebooting. here is my code
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.bootservicestartup.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=".BootUpReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
</application>
public class BootUpReceiver extends BroadcastReceiver {
#SuppressLint("InlinedApi")
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Intent i = new Intent(context, MainActivity.class);
i.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
what the code does is when the phone is rebooted, an activity will be shown. that works. but I also want the activity to be shown even before showing the screen lock.
Hope this helps you if you did it as a service;
You need the following in your AndroidManifest.xml file:
1) In your element:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
2) In your element (be sure to use a fully-qualified [or relative] class name for your BroadcastReceiver):
<receiver android:name="com.example.MyBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
(you don't need the android:enabled, exported, etc., attributes... the Android defaults are correct)
In MyBroadcastReceiver.java:
package com.example;
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent startServiceIntent = new Intent(context, MyService.class);
context.startService(startServiceIntent);
}
}

broadcast receiver error custom intent

I'm doing something I thought would be simple as have done a broadcast receiver in the past on 2.3.4 droid. I'm using 4.x now in this little program.
Here is the main architecture/flow. I want/have a receiver to be listening for a custom broadcast/intent. I created this as a stand along project/apk
public class FileIntegrityMonitor extends BroadcastReceiver
{
public String CLASS_NAME = "FileIntegrityMonitor";
#Override
public void onReceive(Context context, Intent intent)
{
Log.d(CLASS_NAME, "Entered onReceive: got broadcast from startFIM");
String actionReceived = intent.getAction();
Log.d(CLASS_NAME, "Action received=" + actionReceived);
if (actionReceived.equals(StartFIMActivity.CUSTOM_INTENT))
{
//start service to perform the file integrity check
Log.d(CLASS_NAME, "start service to perform file integrity check");
}
}
Then I created a activity as a separate project/app as a driver for now to kick the broadcast receiver off. Will probably replace with something else later but wanted to get the mechanics/comms down now.
public class StartFIMActivity extends Activity
{
/** Called when the activity is first created. */
public static final String CUSTOM_INTENT = "com.kdms2.StartFIM.intent.action.start";
public String CLASS_NAME = "StartFIMActivity";
#Override
public void onCreate(Bundle savedInstanceState)
{
Log.d(CLASS_NAME, "Entered onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent i = new Intent();
i.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); //api 3.1+
i.setAction(CUSTOM_INTENT);
Log.d(CLASS_NAME, "send brodcast to monitor");
this.sendBroadcast(i);
}
}
Now in the trace I do get the intent in FileIntegrityMonitor but a strange message that it's trying to run some method and I don't know why. Error is:
E/AndroidRuntime(979): java.lang.RuntimeException: Unable to instantiate receiver com.kdms2.FileIntegrityMonitor.StartFIMActivity: java.lang.ClassNotFoundException: com.kdms2.FileIntegrityMonitor.StartFIMActivity
why did it add the class name of the activity that broadcast the action to the broadcast receiver?
Is it something in the manifest (receiver)?
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<receiver android:name=".StartFIMActivity" android:enabled="true">
<intent-filter android:priority="99999999999">
<action android:name="com.kdms2.StartFIM.intent.action.start"/>
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
</manifest>
activity manifest
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".StartFIMActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
</application>
</manifest>
v/r,
Frank
Your receiver's name is FileIntegrityMonitor, but in the manifest you try to register StartFIMActivity as both the <activity> and <receiver> elements. Probably oughta be:
<receiver android:name=".FileIntegrityMonitor" android:enabled="true">
<intent-filter android:priority="99999999999">
<action android:name="com.kdms2.StartFIM.intent.action.start"/>
</intent-filter>
</receiver>
Or something of the like.
HTH

Boot/ScreenOn Broadcast Receiver not working

I have a blank HelloWorld Application:
package tutorials.TestReceivers;
import android.app.Activity;
import android.os.Bundle;
public class TestReceiversActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
With this BootReceiver.Java:
package tutorials.TestReceivers;
import android.content.BroadcastReceiver;
public class BootReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent arg1) {
Intent intent = new Intent(context, TestReceiversActivity.class);
context.startActivity(intent);
}
}
and this manifest:
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".TestReceiversActivity"
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-permission="android.permission.RECEIVE_BOOT_COMPLETED"
android:name="development.TestReceiversActivity.BootReceiver" >
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.SCREEN_ON" />
</intent-filter>
</receiver>
</application>
</manifest>
After running the application and closing it.
When I unlock screen (SCREEN_ON) nothing happend.
And when i boot the decive I'm getting next msg like:
"The application TestReceiversActivity (tutorials.TestReceivers process) stop unexpectedly. Try again"
After a long time of frustration, I solved the problem above.
The right way to register Boot Broadcast Receiver (and open activity according to it), is:
Blank HelloWorld Application (TestReceiversActivity.java):
package tutorials.TestReceivers;
import android.app.Activity;
public class TestReceiversActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
Another Boot Receiver Class (BootReceiver.java)
package tutorials.TestReceivers;
import android.content.BroadcastReceiver;
public class BootReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
Log.d("DAVID", "Hi, Boot reciver was catch!");
Intent i = new Intent(context, TestReceiversActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
Note: You must set the flag to make it work!
Set the manifest to:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="tutorials.TestReceivers"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<receiver android:name=".BootReceiver" >
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<activity android:name=".TestReceiversActivity"
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>
Enjoy!
Delete android-permission="android.permission.RECEIVE_BOOT_COMPLETED". Add a <uses-permission> element for this permission as a child of the <manifest> element.
If the problems continue, use adb logcat, DDMS, or the DDMS perspective in Eclipse to look at LogCat and examine the stack trace associated with your crash.
Here is a sample project showing how to get control at boot time.
SCREEN_ON will not work from the manifest.

broadcast receiver never gets called

I'm trying to learn how Broadcast Receivers work but I don't succeed, my receivers are never called. Can you guys point me in the right direction?
Here's the code:
Main.java
public class Main extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = new Intent(getApplicationContext(), OutgoingReceiver.class);
intent.setAction("com.javaGroup.broadcastReceiver.views.TEST");
sendBroadcast(intent);
}
}
OutgoingReceiver.java
public class OutgoingReceiver extends BroadcastReceiver {
public static final String CUSTOM_INTENT = "com.javaGroup.broadcastReceiver.views.TEST";
#Override
public void onReceive(Context context, Intent intent) {
Log.d("HIT OUTGOING-----------------------------------","");
Intent i = new Intent();
i.setAction(CUSTOM_INTENT);
context.sendBroadcast(i);
}
}
IncomingReceiver.java
public class IncomingReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(OutgoingReceiver.CUSTOM_INTENT)) {
Log.d("GOT THE INTENT-------------------------------","");
}
}
}
Manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.javaGroup.broadcastReceiver.views" android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name="com.javaGroup.broadcastReceiver.views.Main" 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=".OutgoingReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"></action>
</intent-filter>
</receiver>
<receiver android:name=".IncomingReceiver" android:enabled="true">
<intent-filter>
<action android:name="com.javaGroup.broadcastReceiver.views.TEST"></action>
</intent-filter>
</receiver>
</application>
</manifest>
Where I'm wrong?
You need to add the right permission in Manifest for your receiver . See list of permissions
Of course you're not going to get it, your BCR (namely OutgoingReceiver) isn't registered to listen to that Intent (com.javaGroup.broadcastReceiver.views.TEST) which your Activity (Main) is broadcasting to.
Note
You should write more comments in your code so that YOU can understand what you are doing. As it stands, I can only guess. You should understand the Activity lifecycle and the full behavior of what you are trying to do. By looking at your code, that's not exactly clear.

Categories

Resources