Reputation: 91
I've total three java files with names MainActivity class, IncomingSMS class, DBManagement class
The IncomingSMS.java receive incoming sms and update inbox. The DBManagement.java have some method like (create, edit, update database etc).
I want to make a database with DBManagement.java class and add all incoming sms details to this database.
The problem is this, when i make an instance of DBManagement class into IncomingSMs class with DBManagement dbManagement = new DBManagement(this);
the context problem arises and notify Context cannot be applied to (com.example.zohaib.ultimatesmsblocker.IncomingSMS.java).
kindly help me to resolve this issue.
IncomingSMS.java
public void onReceive(Context context, Intent intent) {
SmsManager smsManager = SmsManager.getDefault(); // get object and subscription id of SmsManager
Bundle bundle = intent.getExtras(); //Retrieve data from intent activity(sms data) and save to bundle
try {
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus"); //get pdus messages from bundle, cast it to an Object[]
//messageString = "";
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]); //create message from pdu object
String phoneNumber = currentMessage.getOriginatingAddress(); // get sender phone number
String messageBody = currentMessage.getMessageBody(); // get message text
DBManagement dbManagement = new DBManagement(this);
DBManagement.java
package com.example.zohaib.ultimatesmsblocker;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;
public class DBManagement extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "products.db";
private static final String TABLE_NAME = "products";
private static final String COLUMN_ID = "id";
private static final String COLUMN_PRODUCTNAME = "productName";
public DBManagement(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL("CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, PRODUCTNAME TEXT)");
Log.d("Create Database:", "Successful");
} catch (Exception e) {
Log.d("Create Database:", "not");
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
//add new product on a table row
public boolean insertData(String product) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COLUMN_PRODUCTNAME, product);
db.insert(TABLE_NAME, null, contentValues);
return true;
}
public Cursor getAllData() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
return res;
}
}
AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.zohaib.ultimatesmsblocker">
<!-- Give permission to read, receive, send sms -->
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".IncomingSms">
<intent-filter android:priority="10000">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<activity
android:name=".conversation"
android:label="@string/title_activity_conversation"
android:theme="@style/AppTheme.NoActionBar"></activity>
</application>
</manifest>
I want to recieve incoming sms and save it to my own created database.
Upvotes: 1
Views: 6122
Reputation: 949
Pass the context that you get in on receive method.
DBManagement dnmanagement=new DBManagement(context);
Upvotes: 2