上下文不能应用于类

时间:2016-02-17 19:31:18

标签: java android

我总共有三个带有名字的java文件 MainActivity类, IncomingSMS类, DBManagement类

IncomingSMS.java接收传入的短信和更新收件箱。 DBManagement.java有一些方法,如(创建,编辑,更新数据库等)。

我想使用DBManagement.java类创建一个数据库,并将所有传入的sms详细信息添加到此数据库中。

问题在于,当我使用DBManagement dbManagement = new DBManagement(this);将DBManagement类的实例创建到IncomingSMs类时,会出现上下文问题,并且无法将通知上下文应用于(com.example.zohaib.ultimatesmsblocker.IncomingSMS.java)。

请帮我解决这个问题。

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>

我想接收传入的短信并将其保存到我自己创建的数据库中。

1 个答案:

答案 0 :(得分:1)

传递你在get方法中获得的上下文。

DBManagement dnmanagement=new   DBManagement(context);