Unable to compile data when using sharedpreferences in BroadcastReceiver - android

I have an app that I want to have run passivly. I have a settings layout/activity that saves settings to a shared pref (1 string and 1 int). I want to be able to recall the data when the user receives an SMS. Below is my "receiving" activity.
public class PassiveSms extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
SharedPreferences sharedPref= getSharedPreferences("chaosdriver", Context.MODE_PRIVATE);
int speedLimit = sharedPref.getInt("speedLimit", 1000);
String message = sharedPref.getString("message", "I'm sorry, but I am driving. I will text you when I am able!");
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (lastKnownLocation.getSpeed() > speedLimit)
{
Bundle extras = intent.getExtras();
if (extras == null)
{
return;
}
abortBroadcast();
Object[] pdus = (Object[]) extras.get("pdus");
SmsMessage msg = SmsMessage.createFromPdu((byte[]) pdus[0]);
String origNumber = msg.getOriginatingAddress();
onSend(origNumber);
}
}
public void onSend(String px)
{
String reply = "testest";
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(px, null, reply, null, null);
}
}
Here is my manifest:
<?xml version="1.0" encoding="utf-8"?>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver
android:name="biz.midl.drivereply.PassiveLocationChangedReceiver"
android:enabled="true" />
<activity
android:name="biz.midl.drivereply.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>
<activity
android:name="biz.midl.drivereply.Settings"
android:label="#string/title_activity_settings" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="biz.midl.drivereply.PassiveSms"
android:label="#string/title_activity_passive_sms" >
</activity>
<receiver android:name="biz.midl.drivereply.SMSReceiver" >
<intent-filter android:priority="999" >
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<service android:name=".MyService">
<intent-filter>
<action android:name="com.example.MyService"/>
</intent-filter>
</service>
</application>
The "SharedPreferences sharedPref= getSharedPreferences("chaosdriver", Context.MODE_PRIVATE);" is not allowing me to compile because "The method getSharedPreferences(String, int) is undefined for the type PassiveSms"
Any suggestions?

You have to use:
context.getSharedPreferences("chaosdriver", Context.MODE_PRIVATE);
Since SharedPreferences are available only from Context. BroadcastReceiver has proper Context variable as first parameter of onReceive() method so you can use it "without questions".
Now it should works and solves your problem.

Related

Didn't find class on path: DexPathList

I used a receiver in this code after running and receiving a sms this error is shown. I try some thing. I create new project and add file again. delete bin file. clean and restart project. but It doesn't solve the error. Is there anything that can solve this error. I really cant solved it.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="blocker.activity"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".FirstPage"
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=".receiver.SMSReceiver" android:enabled="true">
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<activity
android:name=".Search"
android:label="#string/app_name" >
</activity>
<activity
android:name=".BlockActivity"
android:label="#string/app_name" >
</activity>
<activity
android:name=".SmsFilter"
android:label="#string/app_name" >
</activity>
<activity
android:name=".CustomAdapter"
android:label="#string/app_name" >
</activity>
</application>
</manifest>
error:
property:
the class for sms receiver:
public class SmsFilter extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
Bundle extras = intent.getExtras();
if (extras != null) {
Object[] pdus = (Object[])extras.get("pdus");
if (pdus.length < 1) return; // Invalid SMS. Not sure that it's possible.
StringBuilder sb = new StringBuilder();
String sender = null;
for (int i = 0; i < pdus.length; i++) {
SmsMessage message = SmsMessage.createFromPdu((byte[]) pdus[i]);
if (sender == null) sender = message.getOriginatingAddress();
String text = message.getMessageBody();
if (text != null) sb.append(text);
}
Log.i("LOG: ", "sms recived");
Log.i("LOG: ", sender);
if (sender != null && Search.search(sender)==true) {
abortBroadcast();
}
return;
}
}
// ...
}
}
Please check again the name of the receiver and the class name you declared in your manifest receiver.
In in java file the class name is-SmsFilter extends BroadcastReceiver
SmsFilter extends BroadcastReceiver
As you can see the name is SmsFilter but in your manifest file you are declaring it as SMSReceiver
The names are not matching. Edit your manifest to something like this-
<receiver android:name=".receiver.SmsFilter" android:enabled="true">
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
I hope it would help you..

BroadcastReceiver trying to return result during a non-ordered broadcast error

I have two broadcast receiver. one for sms and one for call.
I confront this error. i see this page:
BroadcastReceiver trying to return result during a non-ordered broadcast - SMS Receiver
but I don't how ti use that suggestion.
Context.sendOrderedBroadcast.
and if this is helpful or not. I have other receiver in this package for sms this receiver work fine. but this one doesn't work.
public class PhoneCallReceiver extends BroadcastReceiver {
Context context = null;
SharedPreferences preferences = null ;
Boolean blacklist;
Boolean contact;
Boolean all;
public void onReceive(Context context, Intent intent) {
preferences = context.getSharedPreferences("modes",Context.MODE_PRIVATE);
if (intent.getAction().equals("android.intent.action.PHONE_STATE")) {
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
String phoneNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
if (preferences.getBoolean("all", true)){
Log.i("all", ": " + phoneNumber);
abortBroadcast();
Log.i("block: ", phoneNumber);
}
else if (preferences.getBoolean("blacklist", true)){
boolean str=Search.search(phoneNumber);
if (phoneNumber != null && str ==true) {
abortBroadcast();}}
else if (preferences.getBoolean("c", true)&&getDetails(phoneNumber)){
abortBroadcast();}
else if (preferences.getBoolean("g", true)){
boolean str=SearchInWhiteList.search(phoneNumber);
if (phoneNumber != null && str ==true) {
abortBroadcast();}}
else if (preferences.getBoolean("b", true)){
abortBroadcast();}
else {}
}
}
}
manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="blocker.activity"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".FirstPage"
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=".SmsFilter" android:enabled="true">
<intent-filter android:priority="999">
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="bloker.activity.android.action.broadcast"/>
<action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
</intent-filter>
</receiver>
<receiver android:name=".PhoneCallReceiver" android:enabled="true">
<intent-filter android:priority="999">
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="bloker.activity.android.action.broadcast"/>
</intent-filter>
</receiver>
<activity
android:name=".Search"
android:label="#string/app_name" >
</activity>
<activity
android:name=".BlockActivity"
android:label="#string/app_name" >
</activity>
<activity
android:name=".SmsFilter"
android:label="#string/app_name" >
</activity>
<activity
android:name=".CustomAdapter"
android:label="#string/app_name" >
</activity>
<activity
android:name=".Setting"
android:label="#string/app_name" >
</activity>
<activity
android:name=".GetAllContact"
android:label="#string/app_name" >
</activity>
<activity
android:name=".WhiteList"
android:label="#string/app_name" >
</activity>
</application>
</manifest>
edit:
broadcastreceiver fro incoming call to android phone is non-ordering. so that cant be aborted using above code. my question is that how can we handle this kind of broadcast?
I know that using this code can block call.
Can I hang up a call programmatically in android?
but my question is that how to use above code to abort call?
You cannot abort the broadcast or programmatically hang up a call. Telephony control can only be done by the Phone app. The broadcasts you are registered to receive are informative only: they tell you about state changes but they are not something which is triggering state changes or driving the telephony state machine.
You can intercept/modify outgoing calls using the ordered broadcast with action of ACTION_NEW_OUTGOING_CALL but there's no public way to intercept an incoming call.

Android SMS Broadcast Receiver not working

I' m trying to build an app that receives notifications of incoming sms. I need help as incoming SMS's are not detected on any device. I am using the following code that I got from here:
http://karanbalkar.com/2014/09/display-incoming-sms-messages-in-android/
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;
public class MySMSApp extends BroadcastReceiver {
public static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
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 strMessageFrom = message.getDisplayOriginatingAddress();
String strMessageBody = message.getDisplayMessageBody();
Toast.makeText(context, "SMS Message received from:" +strMessageFrom, Toast.LENGTH_LONG).show();
Toast.makeText(context, "SMS Message content" +strMessageBody, Toast.LENGTH_LONG).show();
}
}
}
}
And the manifest reads as follows. I have setup the intent receiver to trigger the class above but it seems not to do so. I'm unable to telnet onto the emulator to spoof an incoming sms and so I'm blind as to what's going on.
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/app_name"
android:theme="#style/FullscreenTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<receiver android:name=".SMSBroadcastReceiver" >
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" >
</action>
</intent-filter>
</receiver>
</activity>
</application>
In AndroidManifest.xml you use SMSBroadcastReceiver, but your BroadcastReceiver class name is MySMSApp. Also your <receiver> element is inside the <activity> element.
Try to use this:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/app_name"
android:theme="#style/FullscreenTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".MySMSApp">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>

Android can't understand and make a SmsReceiver for KitKat devices

I followed the Video of DevBytes - Android 4.4 SMS API's - http://www.youtube.com/watch?v=mdq0R2WQssQ
With no luck i couldn't write a successful app that can receive SMS messages for kitkat devices.
I made the exact format that the DevBytes developer did in his video and when i sent a message in the DDMS + Debug my receiver class Didn't jumped to the Breakpoint.
Manifest:
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.WRITE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity android:name="com.example.kitkatreceiver.DefaultAppDeliver">
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<action android:name="android.intent.action.SENDTO" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="sms" />
<data android:scheme="smsto" />
<data android:scheme="mms" />
<data android:scheme="mmsto" />
</intent-filter>
</activity>
<receiver android:name="com.example.kitkatreceiver.KitKatSmsReceiver"
android:enabled="true"
android:permission="android.permission.BROADCAST_SMS">
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_DELIVER"/>
</intent-filter>
</receiver>
</application>
Receiver:
public class KitKatSmsReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
for (SmsMessage smsMessage : Telephony.Sms.Intents.getMessagesFromIntent(arg1)) {
String messageBody = smsMessage.getMessageBody();
Log.d("msg", messageBody);
}
Bundle b = arg1.getExtras();
SmsMessage [] msgs;
if(b != null) {
Object[] pdus = (Object[]) b.get("pdus");
msgs = new SmsMessage[pdus.length];
for(int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
}
for(SmsMessage msg : msgs) {
}
}
}
}
Also i realized that you need to make my app a default app , what if i don't to do that but still catch SMS first instead of the default sms app?
Thanks
Permissions Required :
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS" />
Also, add the permissions just before the <application> tag, not inside the receiver.
Your BroadcastReciever inside the manifest should look like this :
<receiver android:name="com.example.kitkatreceiver.KitKatSmsReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>

My updated SMS Receiver doesn't work Android

hi :) i'm working with SMS in android. i'm having problem with my sms receiver class. when i run my app for the first time on emulator, it works as i've programmed it to work. but when every time i run the app again, it doesn't work as i updated it to work. i'm stuck for last 2 days. can somebody plz guide me or provide some help. my basic receiver class is:
public class SMSreceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
String Sender = null;
String str = "";
SmsMessage [] msgs = null;
if(bundle != null)
{
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]);
Sender = msgs[i].getOriginatingAddress();
str = "SMS From: " + msgs[i].getOriginatingAddress();
str += ":";
str += msgs[i].getMessageBody().toString();
str += "\n";
}
//Toast.makeText(context, str, Toast.LENGTH_LONG).show();
Toast.makeText(context, Sender, Toast.LENGTH_LONG).show();
}
}
in above code, i've commented the toast that shows the msg, and tried to display the toast that shows the sender's number. but still it shows the new msg text. that's weird.
here's my manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.pingpongsmsremote"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="12" />
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.pingpongsmsremote.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>
<activity
android:name="com.example.pingpongsmsremote.SMSScheduler"
android:label="#string/title_activity_smsscheduler" >
</activity>
<activity
android:name="com.example.pingpongsmsremote.FilterSMS"
android:label="#string/title_activity_filter_sms" >
</activity>
<activity
android:name="com.example.pingpongsmsremote.SMSRemote"
android:label="#string/title_activity_smsremote" >
</activity>
<activity
android:name="com.example.pingpongsmsremote.SendSms"
android:label="#string/title_activity_send_sms" >
</activity>
</application>
</manifest>
It looks like maybe you forgot to register the receiver? You need a line like this in your manifest:
<receiver android:name="SMSreceiver" >
<intent-filter>
<action android:name="android.provider.telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
To see it in a full manifest example would look like this:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.pingpongsmsremote"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="12" />
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.pingpongsmsremote.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>
<activity
android:name="com.example.pingpongsmsremote.SMSScheduler"
android:label="#string/title_activity_smsscheduler" >
</activity>
<activity
android:name="com.example.pingpongsmsremote.FilterSMS"
android:label="#string/title_activity_filter_sms" >
</activity>
<activity
android:name="com.example.pingpongsmsremote.SMSRemote"
android:label="#string/title_activity_smsremote" >
</activity>
<activity
android:name="com.example.pingpongsmsremote.SendSms"
android:label="#string/title_activity_send_sms" >
</activity>
<receiver android:name="SMSreceiver" >
<intent-filter>
<action android:name="android.provider.telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
</application>
</manifest>

Categories

Resources