Why app not launching on booting android device? - android

I am testing the following app on HTC with Android 2.3.5. For some reason app is not launching on restarting or booting the phone.
I need to know where am I wrong exactly?
BootReciever.java
import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.widget.Toast;
public class BootupReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "App started", Toast.LENGTH_LONG).show();
/*
Intent startActivityIntent = new Intent(context, MainActivity.class);
startActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(startActivityIntent);
*/
// context.startActivity(new Intent(context, MainActivity.class));
}
}
MainActivity.java
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.content.BroadcastReceiver;
import android.widget.Toast;
import android.content.Intent;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Android Manifest File:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidbootreciever"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.hello_android_world.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
<receiver android:name="com.example.androidbootreciever.BootupReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
</manifest>
The code is working on restart but not on turning on the phone after shutting it down. Here are the messages that I receive in my logcat:

You extended BootupReceiver with Activity?
It should be extended with Broadcast Receiver.
public class BootupReceiver extends BroadcastReceiver{
}

Your Receiver:
public class AutoStartBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent receivedIntent) {
context.startActivity(new Intent(context, YourActivity.class));
}
}
Your Manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name="com.example.receiver.AutoStartBroadcastReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Good luck!

You need to write following permission,
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
If your application is stored on External storage then you should provide following intent filter,
<receiver android:name=".BootupReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
See App Install Location.

Related

Service not starting after boot device

I have a problem with running the service. In the logs there is nothing to see that the service is running. So I don't know that she works. Toast also does not show in MainActivity. I read a lot of posts and none work. How to fix it?
Service
import android.app.Service;
import android.os.IBinder;
import android.widget.Toast;
import android.content.Intent;
public class AutoStartUp extends Service {
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
// do something when the service is created
}
}
BroadcastReceiver
import android.content.Context;
import android.content.BroadcastReceiver;
import android.content.Intent;
public class BootComplete extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) {
Intent serviceIntent = new Intent(context, AutoStartUp.class);
context.startService(serviceIntent);
}
}
}
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="kamiszczu.ovh.servicetest3">
<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:supportsRtl="true"
android:theme="#style/AppTheme">
<receiver
android:name="kamiszczu.ovh.servicetest3.BootComplete"
android:enabled="true"
android:exported="false" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service
android:name="kamiszczu.ovh.servicetest3.AutoStartUp"
android:enabled="true">
</service>
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
You don't have to verify the intent type since the
Receiver is registered with only one intent type(Intent.ACTION_BOOT_COMPLETED). So there is no need to check in the receiver if the intent action is Intent.ACTION_BOOT_COMPLETED.
I think the condition in the receiver is not true and because of that the code that starts your service is not executed.

How to start an app at startup on Android?

I've tried:
This which doesn't work on my phone:
Trying to start a service on boot on Android
This which also fails to work properly:
http://www.compiletimeerror.com/2014/12/android-autostart-app-after-boot-with.html#.VpL6sxWLTIU
And a few others which I couldn't find again. Could someone please post a working example of code which will start a MainActivity with the default HelloWorld xml layout?
Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver android:name=".MyBroadcastReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<activity
android:name=".MyService"
android:label="#string/title_activity_my_service" >
</activity>
</application>
</manifest>
package com.example;
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, MyService.class);
context.startService(startServiceIntent);
}
}
package com.example;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MyService extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_service);
}
}
First:create a BroadcastReceiver, onReceive(Context context, Intent intent),start the app you want to startup in this method
public class BootBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
xxx.class is the app you want to start
Intent service = new Intent(context,XXXclass);
context.startService(service);
Log.v("TAG", "you want to startup this app.....");
Intent intent = getPackageManager().getLaunchIntentForPackage(packageName);
context.startActivity(intent );
} }
Secondly:configure receiver and intent-filter
<receiver android:name="BootBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</receiver>
THirdly,add permission
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

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();
}

start my android app when usb connect

I try to write app which can start when usb connect
I learn from Starting my android application automatically after connecting the USB cable.
my code is
package com.example.formatsdcard;
import java.io.File;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MainActivity extends Activity {
public class OnPowerReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button mButton01 = (Button)findViewById(R.id.button1);
mButton01.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
wipeMemoryCard();
}
});
MY Manifest is
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.formatsdcard"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.formatsdcard.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=".OnPowerReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
</intent-filter>
</receiver>
</application>
my app can run after I click it but it will show there're some problem try again later when I connect my usb cable.
How should I change my code to let it work normally.
You should probably try
<receiver android:name="MainActivity$OnPowerReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
</intent-filter>
</receiver>
Or try putting OnPowerReceiver as a public class in your package com.example.formatsdcard
Create a own class for your onPowerReceiver. Don't put it in your MainActivity.

How to Set new Admin

I am using the Device Policy Manager to lock the android phone immediately and here is my activity code:
package com.husam.admin;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.app.admin.DeviceAdminReceiver;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
public class AdminActivity extends Activity {
/** Called when the activity is first created. */
protected static final int REQUEST_CODE_ENABLE_ADMIN=1;
DevicePolicyManager dpm;
ComponentName mAdminName;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dpm=(DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
mAdminName=new ComponentName(this,MyAdmin.class);
Button button=(Button)findViewById(R.id.admin);
button.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(dpm.isAdminActive(mAdminName))
{
Log.w("Yes admin","Locking Now");
dpm.lockNow();
}
else
{Intent intent1 =new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent1.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mAdminName);
intent1.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "Need new Admin");
Log.w("no Admin","Set admin");
startActivityForResult(intent1,REQUEST_CODE_ENABLE_ADMIN);
}
}
});
}
class MyAdmin extends DeviceAdminReceiver{
void OnEnabled(){
}
void onDisable(){
}
}
}
and My Mainfest.xml fill is as follow:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.husam.admin"
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=".AdminActivity"
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=".MyAdmin"
android:label="#string/device_admin"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data android:name="android.app.device_admin"
android:resource="#xml/my_admin"/>
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED"/>
</intent-filter>
</receiver>
</application>
</manifest>
and my_admin.xml which is inside xml folder under res folder is as follows:
<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
<uses-policies>
<force-lock />
</uses-policies>
</device-admin>
and when I run my app I faced an error in my Log file state that:
W/DeviceAdminAdd(433): Unable to retrieve device policy ComponentInfo{com.husam.admin/com.husam.admin.AdminActivity$MyAdmin}
I searched this website for similar issued but all what I found is that the error was in the mainfest file in closing the receiver before including the meat and intent filter inside it which is not as my case.
So any help or redirection to solve this issue will be completely appreciated
regards
Husam
I think I figured out the answer to the question I posted above. What I need to do is that in registering my receiver in my mainfest file I have to do the following:
<receiver
android:name=".AdminActivity$MyAdmin"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data android:name="android.app.device_admin"
android:resource="#xml/my_admin"/>
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED"/>
</intent-filter>
</receiver>
Note:
in android:name I have to write .AdminActivity$Myadmin instead of .Myadmin, since my class for administration broadcast receiver is an inner class in my activity, not a separate one.

Categories

Resources