Automatically start app on boot from code - android

I have configured my app to automatically start on boot and it's working but only the first time :/
The manifest
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.READ_PRIVILEGED_PHONE_STATE" />
<uses-permission android:name="android.permission.SHUTDOWN"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.PREVENT_POWER_KEY" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.REBOOT" />
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
<uses-permission android:name="android.permission.DEVICE_POWER" />
<receiver android:name=".autostart" android:enabled="true" android:exported="true">
<intent-filter>
<category android:name="android.intent.category.DEFAULT"/>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON"/>
<action android:name="android.intent.action.REBOOT"/>
<!--For HTC devices-->
<action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
</intent-filter>
</receiver>
The autostart
public class autostart 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);
Log.d("AutoStart", "AutoStart");
}
}
I restart and shutdown the app with this code :
public static void Reboot(CaniGPSApplication caniGPSApplication) throws IOException, InterruptedException {
Process proc = Runtime.getRuntime().exec(new String[] { "/system/bin/svc", "power", "reboot" });
proc.waitFor();
}
public static void Shutdown(CaniGPSApplication caniGPSApplication) throws IOException, InterruptedException {
Process proc = Runtime.getRuntime().exec(new String[] { "/system/bin/svc", "power", "shutdown" });
proc.waitFor();
}
I have no idea why my apps doesn't start the second time. I am working on a GPS device (no screen ...) and my app is in system/priv-app .
Hope someone will have an idea :)

I have found a solution.
First I remove autostart block which is not more used
<receiver android:name=".autostart"
And I have added the HOME category in my main activity
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"/>
<category android:name="android.intent.category.HOME"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
From the documentation
This is the home activity, that is the first activity that is
displayed when the device boots.
Perfect for my case : a simple GPS device, but I think a bad idea on a phone :)

Related

GCM notification is not receiving when app is killed( not force kill)

I am happily receiving notifications when app is in background and foreground but when app is killed, I am not receiving notifications in some phones (like Xiaomi model phones eg mi3, etc.)
GCM message 0:1434141725194227%03b66390f9fd7ecd
broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE pkg= (has extras) }
Although I am getting notifications in other phones like nexus, samsung, others.
Did anyone also had similar problem?
Can someone explain me where I am wrong.
Here is my Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="<package_name>">
<permission android:name="<package_name>.permission.C2D_MESSAGE" android:protectionLevel="signature"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="<package_name>.permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<action android:name="android.net.wifi.WIFI_STATE_CHANGED"/>
<action android:name="android.net.wifi.STATE_CHANGE"/>
<application
android:name="<package_name>.ApplicationSingleton"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<activity
android:name="<package_name>.ui.activity.ReferralActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
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="<package_name>.gcm.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="<package_name>" />
</intent-filter>
<intent-filter>
<action android:name="com.google.android.c2dm.intent.REGISTRATION"/>
<category android:name="<package_name>"/>
</intent-filter>
</receiver>
<service android:name="<package_name>.gcm.GcmIntentService" />
</activity>
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
</application>
Autostart probably the best MIUI feature that doesn’t come enabled by default, but should.
What does autostart do? It basically starts the apps you select when the phone starts, so you never miss any notifications or updates.
To enable autostart for your apps, follow the steps below:
Tap on the Security app from the app screen.
You will not find this if you enter from the Settings menu, you have to tap directly on the app icon.
Open Security App:
Once in the Security app, tap on Permissions.
Tap on Autostart
Toggle the apps you want to enable Autostart for
Reboot your phone.
Now you are all set up with autostart!

Starting application on boot completed

The following is the code I'm using for starting my Application when device is turned on.
public class BootReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
Log.i("BootReceiver","intent received");
Intent myIntent = new Intent(context, ACT_Home.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
}
}
and in the Manifest (as <Application> child):
<receiver android:name="host.alarmmanager.BootReceiver">
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
The permissions inside the Manifest are the following:
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.VIBRATE" />
<uses-feature android:name="android.hardware.camera"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
This works fine on Android 3.2.2, but if I try the same application on Android 4.0.3 the broadcast receiver does not receive anything.
Also the first line inside the onReceive method is not execeuted.
Why this happens?
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
This you should use in android manifest
Try this, although your code seems fine! The following is working for me.
<!-- Receivers -->
<receiver android:enabled="true" android:name="host.alarmmanager.BootReceiver"
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>
Make sure you are not restarting your phone by selecting restart option from the power menu.
Android bizarrely has 2 different permissions.
1.Reboot
2.On boot complete
So,
first power off your phone and then after few seconds power on it again!
Hope it helps! (Y)

new Google Cloud Messaging API

I'm sending a push notification using PHP using curl, and the result seems to return fine:
{"multicast_id":2345735671345346,"success":2,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:13456457587969%375ed23445237ecd"},{"message_id":"0:12344526107806%375ed3439fd7ecd"}]}
So I'm guessing the message is sent, and the problem is on the BroadcastReceiver subclass.
My Broadcast receiver code:
package com.example.myexample.pushnotifications;
public class GCMBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
Log.d(TAG, "onReceive method executed properly");
//NotificationManager and NotificationCompat.Builder code to build a notification
}
}
then my AndroidManifest.xml for the permissions:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" /
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission
android:name="com.example.myexample.pushnotifications.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.myexample.pushnotifications.permission.C2D_MESSAGE" />
and for the receiver inside the application tag:
<receiver
android:name="com.example.myexample.pushnotifications.GCMBroadcastReceiver"
android:enabled="true"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.example.myexample.pushnotifications" />
</intent-filter>
</receiver>
The problem is that the broadcast receiver never receives the message.
Assuming the package of the app is com.example.myexample, the following changes are required :
<permission
android:name="com.example.myexample.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.myexample.permission.C2D_MESSAGE" />
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.example.myexample" />
</intent-filter>

Permission Denial: receiving Intent { act=com.google.android.c2dm.intent.REGISTRATION

I'm trying to implement push notifications in my app, but it seems that my C2DMReceiver is not working. I tried every solution I could find. Here is what I have:
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="10" />
<permission android:name="com.my.app.permission.C2D_MESSAGE" android:protectionLevel="signature"/>
<uses-permission android:name="com.my.app.permission.C2D_MESSAGE"/>
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<activity>....</activity>
<service android:name=".C2DMReceiver"/>
<receiver
android:name="com.google.android.c2dm.C2DMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE"/>
<category android:name="com.my.app"/>
</intent-filter>
<intent-filter>
<action android:name="com.google.android.c2dm.intent.REGISTRATION"/>
<category android:name="com.my.app"/>
</intent-filter>
</receiver>
In my onCreate method I use:
C2DMessaging.register(this, "mymail#gmail.com");
String id = C2DMessaging.getRegistrationId(this);
And id is always empty.
Looking at the LogCat, I can see Permission Denial: receiving Intent { act=com.google.android.c2dm.intent.REGISTRATION So, what should I do?Thanks)
Change your android:name in your receiver tag from com.google.android.c2dm.C2DMBroadcastReceiver to com.my.app.YourReceiverClassName, and make sure your sender e-mail and server side login e-mail are the same.
The android:name in your receiver has to specify the path to your class that implements the receiver. I am sure that your C2DMBroadcastReceiver class is not in the com.google.android.c2dm package, as I see below your are using a com.my.app package.
Create a C2DMBroadcastReceiver class in, for example, com.my.app.receiver and specify the receiver's adroid:name as:
<receiver
android:name="com.my.app.receiver.C2DMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
......
</receiver>

My Broadcast Receiver does not start

I have some problem with BroadcastReceiver. There nothing happens when I catch an outgoing call.
public class demoBroadcastReceiver extends BroadcastReceiver {
/** Called when the activity is first created. */
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(context, "there is new calling", Toast.LENGTH_LONG).show();
}
}
This is content of my Manifest:
<uses-sdk android:minSdkVersion="7" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<receiver android:name=".demoBroadcastReceiver">
<intent-filter >
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
<intent-filter >
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
</application>
</manifest>
Updated:
Thanks Lucifer for solution:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED">
I also need this:
<uses-permission android:name="android.permission.NEW_OUTGOING_CALL" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />**strong text**
You should declare this permission in your AndroidManifest.xml file
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
maybe you should post your log here, so we can analyse it.
try add this permission:
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
Hope this
put this in your manifest
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
and this in your application node..
<receiver android:name="com.example.demoBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" /> <--- add this intent filter
</intent-filter>
</receiver>
this is to start a service when a device boots up on android

Categories

Resources