In my app I want to create a service to save constantly a device location. This service has to start when the phone is rebooted.
I changed the Android.Manifest like this
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application android:label="LocationTest">
<receiver>
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
I also changed the received tag like this
<receiver android:permission="RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
I defined a BroadcastReceiver like
[BroadcastReceiver]
[IntentFilter(new[] { Android.Content.Intent.ActionBootCompleted })]
public class BootReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
Toast.MakeText(context, "Received intent!", ToastLength.Short).Show();
Intent i = new Intent(context, typeof(LocationService));
i.AddFlags(ActivityFlags.NewTask);
context.StartService(i);
}
}
I created another one from Xamarin Template like
[BroadcastReceiver]
public class BroadcastReceiverTest : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
Toast.MakeText(context, "New Received intent!", ToastLength.Short).Show();
}
}
If I reboot my device, no one of the BroadcastReceiver is fired. I don't know how I can do that. My project is on GitHub
Thank you in advance.
Although it is possible to find a lot of posts about this question on line, I couldn't find one with all right information. I have created a post for future reference here.
The important thing is in your AndroidManifest: you mustn't declare your broadcast receiver in it. If you have any services, you must declare them in it. For example
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="1" android:versionName="1.0"
package="pro.wordbank.app.locationtest">
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<application android:label="LocationTest">
<service android:name=".TimerService" />
<service android:name=".LocationService" />
</application>
</manifest>
Also, it isn't possible to call Toast.MakeText from your Broadcast Receiver but you have to use instead NotificationManager.
Related
I am trying to develop a ToDo application which helps user make notes as soon as call ends. In general life we are told many things to be done on phone. For example : buy grocery on way back to office.
I am facing difficulty in starting this application. I am using BroadcastReceiver How should I implement onReceive() method?
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.your"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<receiver android:name=".CallTracker">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
</application>
BroadcasrReceiver
public class CallTracker extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
}
}
Look for the 'state' in the bundle. When the state changes from OFFHOOK to IDLE start your application.
From activity you can use this example
I have read several answers about this question, but the posted solution doesn't work for me. Probably there is something wrong or missed in my code.
I need that my app, with no activity, starts automatically after the boot completed.
If I include an activty, just to start for the first time the app (exiting the stopped state), everything works.
Thank you in advance for your help.
Here is my code.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="zag.salva" >
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.READ_CALL_LOG" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver
android:name=".Salva_autostart"
android:enabled="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service
android:name=".Salva_servizio"
android:enabled="true" >
<intent-filter>
<action android:name=".Salva_servizio" />
</intent-filter>
</service>
</application>
Salva_autostart.java
public class Salva_autostart extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Intent intento = new Intent(context, Salva_servizio.class);
intento.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
context.startService(intento);
}
}
Salva_servizio.java
public class Salva_servizio extends Service
{
#Override
public int onStartCommand(Intent intent, int flags, int startId)
{
// Task execution
Salva_invio2 invio = new Salva_invio2();
invio.esegui(this);
return Service.START_NOT_STICKY;
}
#Override
public IBinder onBind(Intent intent)
{
return null;
}
}
You shouldn’t add FLAG_INCLUDE_STOPPED_PACKAGES to your receiver's intent that start your service. You have to add it to the intent that you use for sendBroadcast. Meaning, you need to add it to the intent in the application that invokes the Broadcast.
That is why this flag is irrelevant in your code.
If you will sendBroadcast to this receiver ("Salva_autostart") just once, from outside your application - then your application will not be in "force stop" state any more and on next boot your receiver will be triggered.
Also you should add (addFlags) and not set (setFlags).
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
This is how you should trigger your receiver from another application:
Intent intent = new Intent("com.xxx.my_filter_intent");
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
this.sendBroadcast(intent);
On your manifest add the above filter intent to your receiver (you can add it in new <intent-filter> or in the same that you already have for the BOOT_COMPLETED action.
<receiver
android:name=".Salva_autostart"
android:enabled="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
<intent-filter>
<action android:name="com.xxx.my_filter_intent" />
</intent-filter>
</receiver>
Read more here:
http://developer.android.com/about/versions/android-3.1.html#launchcontrols
Note that as of Android 3.0 the user needs to have started the application at least once before your application can receive android.intent.action.BOOT_COMPLETED events.
I am working on one app which will start a service automatically at device start ( after boot complete ). i did code but i think something is going wrong with my code. i have tried google too and all the tricks while research. can you please help me? Thanx in advance.
here is my code :
My BroadcastReceiver is
public class AutostartReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Toast.makeText(this, "Booting Completed", Toast.LENGTH_LONG).show();
//context.startService(new Intent(context, MyService.class));
}
}
}
My AndroidMenifest file is
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myhiddenapp"
android:installLocation="internalOnly"
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/ic_launcher"
android:label="MyHiddenApp" >
<receiver
android:name=".AutostartReceiver"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name=".MyService" />
</application>
</manifest>
Note : I have not any Activity, i just want to start Service on Boot Complete.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:installLocation="internalOnly"
... >
You Must set android:installLocation="internalOnly"
https://developer.android.com/guide/topics/data/install-location.html#Should
I am currently trying to create a phone number verification on registering on my Android app. Thank you for your help to read my long post!
Here is my Manifest file with other activities omitted
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.opensem"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<application
android:name=".AppHelper"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme"
>
<receiver android:name=".SMSReceiver">
<intent-filter android:priority="2147483647">
<action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
</intent-filter>
</receiver>
<activity
android:name=".OpenSem"
android:label="#string/title_activity_opensem" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Here is my class which I want to show a log on receive SMS
public class SMSReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
Log.d("TAG", "Received");
}
}
Here is my class sending the SMS
public class RegisterPageSubmitListener implements OnClickListener{
public void onClick(View v) {
SmsManager sm = SmsManager.getDefault();
number = ParamList.get("phone");
code = randInt(100000, 999999) + "";
sm.sendTextMessage(number, null, code, null, null);
}
}
My problem is that I successfully send the SMS and received it on my phone, but the "Received" Log was never shown. I came across the priority problem on StackOverflow and thus I added the android:priority="2147483647" in my manifest. I then added these lines (Copied from another SO post) in other activities to see if my receiver is properly registered.
Intent intent = new Intent("android.provider.Telephony.SMS_RECEIVED");
List<ResolveInfo> infos = getPackageManager().queryBroadcastReceivers(intent, 0);
for (ResolveInfo info : infos) {
Log.d("OpenSEM", "Receiver name:" + info.activityInfo.name + "; priority=" + info.priority);
}
and the resulting log is shown below:
08-01 15:27:10.417: D/OpenSEM(12880): Receiver name:com.android.mms.transaction.HighPrivilegedSmsReceiver; priority=1000
08-01 15:27:10.417: D/OpenSEM(12880): Receiver name:com.example.opensem.SMSReceiver; priority=999
08-01 15:27:10.417: D/OpenSEM(12880): Receiver name:com.android.mms.transaction.PrivilegedSmsReceiver; priority=0
It seems that my receiver's priority was set to 999 instead of 2147483647. Also, I didn't install any SMS applications, so the other priority=1000 receiver seems to belong to the system messaging application.
Did I do anything wrong? I tested my code on a Red MI (MIUI) and Samsung note II. My SMSReceiver seems to be registered and not receiving the SMS which was successfully sent back to both phones. I would really appreciate any help!!
Potato. For the long post.
It may be the restrict of your ROM, that is MIUI, I have encountered the problem that my BroadcastReceiver can't receive broadcast, but my app is OK on SamSung phone.
I have a service in my Android app that works perfectly if i started manually, but i need that service to start on boot, and i can't get it started automatically after rebooting the phone, i tryed several tutorials but nothing works.
Here is my manifest code:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tvshowsguide"
android:versionCode="1"
android:versionName="1.0"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
<service android:name="androidservice.SyncService" />
<receiver android:name="androidservice.SyncAuto" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
Here is the BroadcastReceiver code:
public class SyncAuto extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Intent SyncService = new Intent(context, SyncService.class).setAction(SRVC);
context.startService(SyncService);
}
}
}
I think the problem must be the reference of your service, since you have an application with
package="com.tvshowsguide"
Your service and receiver must have a name like:
<service android:name="com.tvshowsguide.androidservice.SyncService" />
<receiver android:name="com.tvshowsguide.androidservice.SyncAuto" >