am trying to write an sms body into a shared preference file when onReceive method is invoked, but seems not to work. i have a class receiverClass which extends BroadcastReceiver and SessionManager is my shared preference class below is what i tried which doesn't work if i declare sessionManager globally else it displays the toast msg when it actually receives a new msg when SessionManager isn't declared.
public class receiverClass extends BroadcastReceiver{
SessionManager session;
@Override
public void onReceive(Context context, Intent intent) {
String sendingNumber="";
String smsBody="";
Bundle bundle=intent.getExtras();
SmsMessage[] msgs = null;
String smsBody1="";
if (bundle!=null) {
Object[] pdus=(Object[])bundle.get("pdus");
msgs=new SmsMessage[pdus.length];
for (int i = 0; i < pdus.length; i++) {
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
sendingNumber += msgs[i].getOriginatingAddress();
smsBody1=msgs[i].getMessageBody().toString();
}
Toast.makeText(context, "Remote Access " + smsBody1, Toast.LENGTH_SHORT).show();
session.onSmsBody(sendingNumber,smsBody1);
}
}
however i also tried with sqlite database which crashes my application when a new sms is received even if the application is not opened. below is code with sqlite database and its class
public class DBController extends SQLiteOpenHelper{
private static final String LOGCAT = null;
public DBController(Context receiverClass) {
super(receiverClass, "SMSsqlite.db", null, 1);
Log.d(LOGCAT,"Created");
}
@Override
public void onCreate(SQLiteDatabase database) {
String query;
query = "CREATE TABLE SMS_configuration ( smsId INTEGER PRIMARY KEY, originatingAdress TEXT, smsBody TEXT)";
database.execSQL(query);
Log.d(LOGCAT,"SMS_configuration Created");
}
@Override
public void onUpgrade(SQLiteDatabase database, int version_old, int current_version) {
String query;
query = "DROP TABLE IF EXISTS SMS_configuration";
database.execSQL(query);
onCreate(database);
}
public void insertSms(HashMap<String, String> queryValuesOrig, HashMap<String, String> queryValuesBody) {
SQLiteDatabase database = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("originatingAdress", queryValuesOrig.get("originatingAdress"));
values.put("smsBody", queryValuesBody.get("smsBody"));
database.insert("SMS_configuration", null, values);
database.close();
}
}
and receiverClass Class
public class receiverClass extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String sendingNumber="";
String smsBody="";
Bundle bundle=intent.getExtras();
SmsMessage[] msgs = null;
String smsBody1="";
if (bundle!=null) {
Object[] pdus=(Object[])bundle.get("pdus");
msgs=new SmsMessage[pdus.length];
for (int i = 0; i < pdus.length; i++) {
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
sendingNumber += msgs[i].getOriginatingAddress();
smsBody1=msgs[i].getMessageBody().toString();
}
Toast.makeText(context, "Remote Access " + smsBody1, Toast.LENGTH_SHORT).show();
addNewConfig(sendingNumber,smsBody1);
}
}
public void addNewConfig(String sendingNumber, String smsBody1) {
DBController controller = new DBController(null);
HashMap<String, String> queryValuesOrig = new HashMap<String, String>();
HashMap<String, String> queryValuesBody = new HashMap<String, String>();
queryValuesOrig.put("originatingAdress", sendingNumber.toString());
queryValuesBody.put("smsBody", smsBody1.toString());
controller.insertSms(queryValuesOrig,queryValuesBody);
}
}
and my manifest file looks like this
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
receiver android:name=".receiverClass" android:exported="true">
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
all i want to do is write the sms body onReceive into a shared preference file or sqlite database where i would be using later and application is being runned on a kitkat device and am also aware of the security issues but is there's a solution for this i would gladly need ty.
Aucun commentaire:
Enregistrer un commentaire