Using android device as a remote control - android

I want to make an aaplication that has the feature of controlling my phone from remotely.Such as on/off GPS through text message from another phone.Getting the location from phone via text message.
Is it possible to on/off gps through text message from another phone?

Definitely yes.
You just need to add in you application a receiver for the Action android.provider.Telephony.SMS_RECEIVED and parse the message text if you recognize ac command in your phone, you execute some code.
Basically you need to do three things:
Update you manifest abd add permission for your application to receive SMS:
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
Then you have to add, again in the manifest a receiver, that receive the SMS_RECEIVED Action, in a way similar to the following:
<receiver android:name=".SMSReceiver" android:enabled="true" >
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
Where android.name is the name of your Receiver class.
And finally you have to implement that class, that extends BroadCastReceiver and has at least the onReceive Method implemented.
public class SmsReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
}
}
For your help, below there an example onReceive code:
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
Object[] pdus = (Object[])bundle.get("pdus");
SmsMessage[] messages = new SmsMessage[pdus.length];
for(int i = 0; i < pdus.length; i++){
messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
}
for(SmsMessage message: messages){
String messagestr = message.getMessageBody();
String sender = message.getOriginatingAddress();
Toast.makeText(context, sender + ": " + messagestr, Toast.LENGTH_SHORT).show();
}
}
That code, read the message content and show it on a Toast. You can find a full working example here: https://github.com/inuyasha82/ItalialinuxExample/tree/master/LezioniAndroid

Recieving (handling) SMS-es is possible with Android. Your software should read the SMS, then decide if it contains command and turn off the GPS like normal app would.
This link shows how http://www.apriorit.com/dev-blog/227-handle-sms-on-android

Related

How to read SMS code from your android App

We are looking to build the functionality in our app to read a security code that being sent as part of SMS and display on the textView.
Also, I am not looking to build a broadcast receiver, may be an intent service which only will start run on a particular screen and kill the service once user navigated to another screen.
It would be great if anyone can shade some light and help with some sample code.
To read incoming SMS you have to do three things.
Broadcast Receiver
Declare Broadcast Receiver in manifest
Need SMS Receive permissions
Note: If you are compiling against 6.0 Marshmallow you have get android.permission.RECEIVE_SMS at runtime. Runtime Permissions
Lets Starts Receiving incoming SMS
1) First add permissions in manifest
<uses-permission android:name="android.permission.RECEIVE_SMS" />
2) Declare Broadcast Receiver in Manifest.
What this declaration do it will inform you when ever a new SMS Receive by device.
<receiver android:name="com.example.abc.ReciveSMS">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
3) Add this code to your declared class in manifest
public class ReciveSMS extends BroadcastReceiver{
private SharedPreferences preferences;
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
Bundle bundle = intent.getExtras(); //---get the SMS message passed in---
SmsMessage[] msgs = null;
String msg_from;
if (bundle != null){
//---retrieve the SMS message received---
try{
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for(int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
msg_from = msgs[i].getOriginatingAddress();
String msgBody = msgs[i].getMessageBody();
}
}catch(Exception e){
// Log.d("Exception caught",e.getMessage());
}
}
}
}
}
Original Post here.

display android TOAST on receive of sms

i am working on an app which shows a toast on sms is received.i want either sms content to be displayed or just an notification that sms has been received.
I am using android studio. and am new to it. please help..
Register a receiver which listens to new message.
Refer these link
Android - SMS Broadcast receiver
and Blog : Incomming SMS Broadcast Receiver - Android Example
Use the BroadcastReceiver to listen to the sms received. Codes are here:
public class SmsReciver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
SmsMessage msg = null;
if (null != bundle) {
Object[] smsObj = (Object[]) bundle.get("pdus");
for (Object object : smsObj) {
msg = SmsMessage.createFromPdu((byte[]) object);
Date date = new Date(msg.getTimestampMillis());//时间
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String receiveTime = format.format(date);
Toast.make(context, "number:" + msg.getOriginatingAddress()
+ " body:" + msg.getDisplayMessageBody() + " time:"
+ msg.getTimestampMillis(), 1000).show();
}
}
}
}
Meanwhile, add this in your AndroidManifest.xml:
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
...
<receiver android:name="com.dbjtech.acbxt.waiqin.SmsReciver" >
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
If your app is running on an Android version > 3.0, you'll have to include an Activity that the user can start manually after installation to bring the app out of its stopped state, a concept introduced in Android 3.1. The system will not broadcast to stopped apps, so your BroadcastReceiver won't work until the app has been explicitly launched by the user.
Apart from that, your code appears to be correct.

String message of an SMS

I am learning how to develop Apps on Android.
I have a button that onClick, it should read my SMS and check the body of each SMS, if one SMS contains "WORD" then DO something.
It should be achieved using this, right?
String body = sms.getMessageBody().toString();
while (body.contains("WORD"))
DO SOMETHING
Thanks in advance.
EDIT
Is there any way to do this without hitting the button, progamatically every hour for example?
if you want ot check it with new message you need to create BroadcastReceiver
AndroidManifest.xml Declaration :
<receiver android:name=".MySMSReceiver ">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
BroadcastReceiver :
public class MySMSReceiver extends BroadcastReceiver {
#Override
public void onReceiver(Context context, Intent intent) {
Object[] pdus=(Object[])intent.getExtras().get("pdus");
String sender="";
StringBuilder text=new StringBuilder();
// get sender from first PDU
SmsMessage shortMessage=SmsMessage.createFromPdu((byte[]) pdus[0]);
sender=shortMessage.getOriginatingAddress();
for (int i=0;i<pdus.length;i++) {
shortMessage=SmsMessage.createFromPdu((byte[]) pdus[i]);
text.append(shortMessage.getDisplayMessageBody());
}
while (text.contains("WORD")) {
// DO SOMETHING
}
Log.d("SMSReceiver","SMS message sender: " + shortMessage.getOriginatingAddress());
Log.d("SMSReceiver","SMS message text: " + shortMessage.getDisplayMessageBody());
}
}
instead of while you need to use an if statement...
if (body.contains("WORD"){
DO SOMETHING
}
You can use AlarmManager to schedule some task to be run at some point in the future
See https://stackoverflow.com/a/8801990/1051147 for example

Listening to incoming texts, in the background

I'm trying to make my application listen to incoming text messages, in a persistent manner. What would be the best approach for this?
Currently, I've got a working BroadcastReceiver and I'm playing around with implementing a local service for my app. Is somehow implementing the BroadcastReceiver into the service the correct way of doing this? Will the service work in a low memory condition?
Yes broadcast receiver is best way for listening to incoming texts.if an incoming sms recvied use IntentService for your work what u want to do on sms recived.u can register a reciver for incoming sms as:
manifest file
<receiver class="SMSApp">
<intent-filter>
<action android:value="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.RECEIVE_SMS" />
Java File
public class SMSApp extends BroadcastReceiver{
private static final String LOG_TAG = "SMSApp";
/* package */
static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";
public void onReceive(Context context, Intent intent){
if (intent.getAction().equals(ACTION)){
Bundle bundle = intent.getExtras();
if (bundle != null){
Object[] pdus = (Object[]) bundle.get("pdus");
SmsMessage[] messages = new SmsMessage[pdus.length];
for (int i = 0; i < pdus.length; i++){
messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
}
for (SmsMessage message : messages){
String strFrom = message.getDisplayOriginatingAddress();
String strMsg = message.getDisplayMessageBody();
}
}
}
}
}
and second way you can register a ContentObserver for content://sms/inbox for listening incoming sms in inbox
The broadcast receiver does not need your application to be started, hence is the correct way of listening to incoming texts.
Just make sure to register it in the Manifest

How to handle Service Information in Android

Operator can send Service Information message to phone user. It appears as notification window. My operator for example sends spent and left money after every call. It uses these Service Information for that.
Is there a way to handle that message?
If they are SMS (The case I see with operators around me).
Simple add an intent filter to your manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.example">
<uses-permission id="android.permission.RECEIVE_SMS" />
<application>
<receiver class="SMSApp">
<intent-filter>
<action android:value="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
and just extends android.content.IntentReceiver and implement the onIntentReceived method as follows:
public void onReceiveIntent(Context context, Intent intent) {
if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
Bundle bundle = intent.getExtras();
if (bundle != null) {
SmsMessage[] messages = Telephony.Sms.Intents.getMessagesFromIntent(intent);
for (int i = 0; i < messages.length; i++) {
SmsMessage message = messages[i];
System.out.println("Received SMS from: "+message.getDisplayOriginatingAddress());
System.out.println(message.getDisplayMessageBody());
}
}
}
I think that in the similar way parsing of USSD services is working. Have a look into the class that parses USSD reply: http://codepaste.ru/7545/. I hope this will help you. Good luck!

Categories

Resources