I am trying to get my app to auto start on boot up and it will and an error occurs while launching the application
Here is my manifest and the program file for the "Auto Starter":
manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="this.bad.file"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name="autoBot"
android:enabled="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
Here is the "AutoBot" as I have called it (Not for spammy reasons I just like transformers):
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class autoBot extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Intent startUp = new Intent(context, MainActivity.class);
context.startActivity(startUp);
}
}
So there we have it!
In your manifest, you left out a vital name of the class, usually, can be abbreviated to [dot][ClassName] or the full package name, as in for example .autoBot or see the example below:
<receiver android:name="this.bad.file.autoBot">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
And in your Broadcast receiver:
public class autoBot extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent startUp = new Intent(context, MainActivity.class);
context.startActivity(startUp);
}
}
Notice the usage of #Override on the onReceive class.
The normal recommended route to take is usually, to start an alarm on boot, and listen for the broadcast when the alarm has expired, in that way your activity does not hog up the boot and also, allow the boot up process to "settle" for a bit.
Related
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.
I have two launcher activities,
1. Receiver activity which is a Broadcast Receiver.
2. An Activity which should be main Launcher activity.
When an SMS arrives the receiver activity launches the .MainActivity(BroadcastReceiver) and it further start one service. (Without GUI) and without opening Settings activity.
Another activity .Settings is Main launcher activity.
Goal I want to Achive:
When sms arrived receiver activity work as it is. But When I want to change the settings Setting activity starts.
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.aa"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.RECEIVE_SMS" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.aa.FindLocation"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.aa.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.aa.Settings"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.aa.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".MainActivity"
android:enabled="false" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</receiver>
<service android:name=".Servc" >
<intent-filter>
<action android:name="com.example.Servc" />
</intent-filter>
</service>
</application>
</manifest>
Problem
1.
[2014-07-06 02:55:14 - Aa] Uploading Aa.apk onto device 'S5830f4524b76'
[2014-07-06 02:55:14 - Aa] Installing Aa.apk...
[2014-07-06 02:55:18 - Aa] Success!
[2014-07-06 02:55:18 - Aa] Starting activity com.example.aa.Settings on device S5830f4524b76
[2014-07-06 02:55:19 - Aa] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.aa/.Settings }
2.
What changes should be made in manifest to make it working?
Don't define your sms receiver in the manifest. Remove it.
Create Broadcast receiver that runs on boot up by defining the below element in your manifest :
<receiver android:name=".BootUpReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
You will need to add this permission to your manifest :
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
Then register your sms receiving BroadcastReceiver in there :
public class BootUpReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
SmsReceiver smsreceiver = new SmsReceiver();
IntentFilter fltr_smsreceived = new IntentFilter(
"android.provider.Telephony.SMS_RECEIVED");
registerReceiver(smsreceiver, fltr_smsreceived);
}
}
SmsReceiver is your broadcast receiver. For example :
public class SmsReceiver extends BroadcastReceiver {
void setActivityHandler(){
//instantiate member variables if required
}
#Override
public void onReceive(Context context, Intent intent) {
Bundle pdusBundle = intent.getExtras();
Object[] pdus=(Object[])pdusBundle.get("pdus");
SmsMessage messages=SmsMessage.createFromPdu((byte[]) pdus[0]);
Log.d("tag","message body : " + messages.getMessageBody());
}
}
i'm having trouble with the auto-start feature of my APP.
I've searched trhough the forums, seen many suggestions, none seems to work and I don't know why.
Here's the BootUpReciver.java
package com.???.???;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class BootUpReciver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
//Intent pushIntent = new Intent(context, BackgroundService.class);
//context.startService(pushIntent);
Toast.makeText(context, "Device Booted", Toast.LENGTH_LONG).show();
Log.d("TAG","Device Booted");
}
}
}
And here's my Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.???.???"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<application
android:theme="#android:style/Theme.Holo"
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<receiver android:name=".BootUpReciver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<activity
android:name="com.???.???.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>
When i restart my phone i don't even see the Toast, why? what should i do? I need to auto-start to get some sharedprefs and write a value to a file every boot.
Thanks.
While testing I found that If I show toast in onReceive then it is not showing. But if I start an activity in onReceive then it is working. So you can also check this. Start an activity or service and perform your task there.
I am new in android.I had started one service on bootup completed of device in my project but i am not having idea about how do i attach debugger for it.i.e i want to debug my code for some bugs so how should i proceed for that?please help me out.Thanks in advance
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.broadcast.receiver.example"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="#drawable/icon" android:label="#string/app_name" android:debuggable="true">
<activity android:name=".BR_Example"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Declaring broadcast receiver for wifi status changing event -->
<receiver android:name=".MyReceiver " android:enabled="true" android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
</manifest>
public MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent myIntent = new Intent(context, YourService.class);
context.startService(myIntent);
}
i hope it will help you man ............
I am new to android development, and
I have an app that when run from the app icon in the menu / home screens runs just fine. However when I try to make it run on boot completion it crashes (Keeping in mind that if I go back to the Icon it will still work) I am wondering what is wrong here. Keep in mind I am doing testing on my personal phone which is android 2.3 and it is ROOTED. Some how I don't think this should be as hard as it is to do, since 9/10 most android apps work in this way or in a way similar to this. Simply put: What am I doing wrong?
In the mean time here is my code for the Broadcast Receiver:
package path.to.file;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class autoBot extends BroadcastReceiver {
private static final String LOG_TAG = "StartAtBootServiceReceiver";
#Override
public void onReceive(Context context, Intent intent) {
Log.e(LOG_TAG, "onReceive:");
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Intent i = new Intent();
i.setAction("path.to.file.MainActivity");
context.startActivity(i);
}
}
}
As well as the Manifest file:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
installlocation="internalOnly"
package="path.to.file"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="autoBot" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" >
</action>
<category android:name="android.intent.category.HOME" >
</category>
</intent-filter>
</receiver>
</application>
</manifest>
Is there a specific reason you are adding an action to your intent? If your only aim is to launch your activity at start-up, do this in your BroadcastReceiver:
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
Also, remove these lines from your manifest under receiver tag, you won't need them there:
<category android:name="android.intent.category.HOME" ></category>