I hope an app can forward SMS automatically when an SMS arrives. I think the service need to keep run in foreground to monitor SMS.
And I have to start and stop the service,and the service starts automatically when moblie phone power on. How can I do this? Thanks!
Add android:permission="android.permission.RECEIVE_BOOT_COMPLETED" to your manifest and Create a BroadcastReceiver like following:
public class autostart extends BroadcastReceiver
{
public void onReceive(Context arg0, Intent arg1)
{
Intent intent = new Intent(arg0,yourService.class);
arg0.startService(intent);
Log.i("Autostart", "started");
}
}
You can monitor the sms using this piece of code.Create a Broadcast reciever as Yup said and send sms using service.
<receiver android:name=".SmsReceiver" android:exported="true" >
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
Related
I want to make my service always working but in the normal service if the user close the phone and open it or restart it the service is stoping can you help me. thanks
You could use a Broadcast Receiver that would take the permission to broadcast a message on the restart of the phone that would tell the service to be started or as we call it in technical terms we would use an intent-filter having the action of starting the service when in the actions (or the ) the boot process is completed.
In manifest file :-
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
In application tag of manifest.xml :-
<receiver android:name="com.example.MyBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
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);
}
}
Now, at the end the service class of MyService would be started by this boradcasting.
This should be fairly easy but I somehow can't get a Broadcast receiver's onReceive method triggered. Details below:
App B provides a broadcast receiver.
Manifest:
<receiver android:name=".MyNotificationReceiver">
<intent-filter>
<action android:name="com.go.foo.A_ACTION" />
</intent-filter>
</receiver>
Java:
public class MyNotificationReceiver extends BroadcastReceiver {
private final String TAG= "MyNotificationReceiver";
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "this is not shown" , Toast.LENGTH_LONG).show();
}
}
App A is Broadcast Sender App:
Java
Intent intent = new Intent();
intent.setAction("com.go.foo.A_ACTION");
sendBroadcast(intent);
Log.d(TAG, "broadcast intent sent...");
I can see the log statement that the broadcast is sent but the receiver's onReceive() callback is not getting triggered. Am I doing something wrong?
Interesting why it's not working. Try this one out. I know the default values of exported and enabled are true. But still have a try at it.
<receiver android:name=".MyNotificationReceiver" android:enabled="true" android:exported="true">
<intent-filter>
<action android:name="com.go.foo.A_ACTION" />
</intent-filter>
</receiver>
Learned something new today. As of Android 4.1, the system no longer sends broadcasts to app components without an activity. The broadcast received started working after I added an activity in the manifest.
I want to Start a service when phone reboot completed. I will install my app but will not launch it.
I have proceed with the following ways using Broadcast Receiver listening to BOOT_COMPLETED intent.
My Receiver is as follows...
public class MyReceiver extends BroadcastReceiver {
static final String TAG = "MyReceiver";
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.d(TAG, "******* OnReceive");
Log.d(TAG, "Boot Completed Caught");
context.startService(new Intent(context, MyService.class));
}
}
In Manifest I have added the following lines of code:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<service android:name="MyService"
android:enabled="true" >
<intent-filter>
<action android:name="com.example.logic.MyService" />
</intent-filter>
</service>
<receiver android:name="MyReceiver" android:enabled="true" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
But receiver is not listening once boot is completed. Remember I have only installed my app but never launch it.But my requirement is such that I should not launch my app after installing it in my device. Anybody can help me on this.
I googled it and found that as I have not launched my app it is in stopped state and boot completed intent is ignoring my receiver. The below links that might help you in understanding my issue:
http://devmaze.wordpress.com/2011/12/05/activating-applications/
the service and MyReceiver must be full packagename,
like this
I'm developing an Android application, i have a service running in background that i want to lauch it by introducing a custom ussd number. For example when i call #12345# my service starts.Thank you in advance
You can register a BroadcastReceiver in your manifest that listens for android.provider.Telephony.SECRET_CODE intents. You also specify the secret code in the manifest.
For example, this manifest entry registers MyBroadcastReceiver for the secret code *#*#12345#*#*.
<receiver android:name="MyBroadcastReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SECRET_CODE"/>
<data android:scheme="android_secret_code" android:host="12345"/>
</intent-filter>
</receiver>
MyBroadcastReceiver should look something like this:
public class MyBroadcastReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
// Create an intent to start your Service.
}
}
I have an Android service which sends broadcast intents. I'm trying to get those intents in another application, which is an Android service. I wrote this in my manifest:
<!-- Service -->
<service android:enabled="true" android:name="...MyService"></service>
<!-- Receiver -->
<receiver android:name="...MyReceiver">
<intent-filter>
<action android:name="..."></action>
<action android:name="..."></action>
</intent-filter>
</receiver>
and this in my MyReceiver class:
public class ScannerBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// Process action.
Log.d(Globals.LOG_TAG, "Intent received.");
...
Unfortunately I never get the onReceive method invoked. Any idea why?
I start this service from another test application, so this is set as an Android library. The service is correctly started but this receiver is receiving nothing. Any idea what I'm doing wrong?
Thanks!
In manifest must be: android:name=".[package].ScannerBroadcastReceiver"
I solved the problem and I suppose it is due to the fact that this project was set as a library. If I don't set it this way the intent is correctly received. I haven't read about this anywhere.