-1

ここに私のdatabase.javaファイルがあります

 package com.example.mobile_computing_project;



import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class Database_Helper extends SQLiteOpenHelper {

public Database_Helper(Context smsReceiver) {
    super(smsReceiver, dbName, null, 1);
    // TODO Auto-generated constructor stub
}
static final String dbName="MySms";
static final String Login_Table="Login_Authentication";
static final String colID="ID";
static  final String colUsername="Username";
static final String colPassword="Password";
static final String Sms_table = "Sms_Repository";
static final String number = "Cell_Num";
static final String message = "message_text";

@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + Login_Table + "(" + colID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + colUsername + " TEXT NOT NULL," + colPassword + " TEXT NOT NULL);");
db.execSQL("CREATE TABLE " + Sms_table + "(" + number + " TEXT NOT NULL, " + message + " TEXT NOT NULL);");
Log.w("Come aww man", "Database Table Created!!");


}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + "Login_Authentication");
db.execSQL("DROP TABLE IF EXISTS " + "Sms_table");
onCreate(db);
}
public void insert_new_user(String username,String password)
{
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues cv = new ContentValues();
    cv.put(Database_Helper.colUsername,username);
    cv.put(Database_Helper.colPassword,password);
    db.insert(Login_Table, null, cv);
    db.close();
}
public void insert_text(String number, String text)
{
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues cv = new ContentValues();
    cv.put(Database_Helper.number,number);
    cv.put(Database_Helper.message,text);
    db.insert(Sms_table, null, cv);
    db.close();

}
public void delete(long id)
{
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(Login_Table, colID+"="+id, null);
}
public String getinfo() {
    // TODO Auto-generated method stub
    String[] columns  =  new String[]{ colID,colUsername,colPassword};
    SQLiteDatabase bm = this.getReadableDatabase();
    String res = " ";
    Cursor c = bm.query(Login_Table, columns, null, null, null, null, null);
    int irow = c.getColumnIndex(colID);
    int iuser = c.getColumnIndex(colUsername);
    int ipass = c.getColumnIndex(colPassword);
    for(c.moveToFirst(); !c.isAfterLast();c.moveToNext() )
    {
        res = res + c.getString(irow) + " " + c.getString(iuser) + " " + c.getString(ipass) + "\n" ;
    }

    return res;
}
public String get_texts() {
    // TODO Auto-generated method stub
    String[] columns  =  new String[]{ number,message};
    SQLiteDatabase bm = this.getReadableDatabase();
    String res = " ";
    Cursor c = bm.query(Sms_table, columns, null, null, null, null, null);
    int inum = c.getColumnIndex(number);
    int imessage = c.getColumnIndex(message);

    for(c.moveToFirst(); !c.isAfterLast();c.moveToNext() )
    {
        res = res + c.getString(inum) + " " + c.getString(imessage)+ "\n" ;
    }

    return res;
}
public void updating_the_table(long id, String user_updating) {
    // TODO Auto-generated method stub
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues cv_update = new ContentValues();

    cv_update.put(Database_Helper.colUsername, user_updating);
    long test = db.update(Login_Table, cv_update, colID+"="+id, null);
    db.close();

}
public String verify_user(String is_user)
{
    String verify = " ";
    String check = " ";
    SQLiteDatabase fg = this.getReadableDatabase();
    Cursor c = fg.query(Login_Table, null, null, null, null, null, null);
    int iuser = c.getColumnIndex(colUsername);
    for(c.moveToFirst() ; !c.isAfterLast();c.moveToNext())
    {
        verify = c.getString(iuser);
        if(is_user.equals(verify))
        {
            check = "Valid User!";
        }
    }
    return check;

}
}

ここにファイル SmSReceiver.java があります

package com.example.mobile_computing_project;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsMessage;
import android.util.Log;
import android.widget.Toast;
import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.widget.TextView;

public class SmsReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent) 
{
    //---get the SMS message passed in---
    Bundle bundle = intent.getExtras();        
    SmsMessage[] msgs = null;
    String str = "";            
    if (bundle != null)
    {
        //---retrieve the SMS message received---
        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]);  
            Database_Helper accessing_db = new Database_Helper(SmsReceiver.this);
               accessing_db.insert_text(msgs[i].getOriginatingAddress().toString(), msgs[i].getMessageBody().toString());
            accessing_db.close();
            Log.w("Ho gya!", "Welcome User jee!");
            Toast.makeText(context, "New User Has Been Created!!", Toast.LENGTH_SHORT).show();
            str += "SMS from " + msgs[i].getOriginatingAddress();                     
            str += " :";
            str += msgs[i].getMessageBody().toString();
            str += "\n";        
        }
        //---display the new SMS message---
        Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
    }                       
}
}

私のプログラムが次の行でエラーを表示する理由がわかりません:

Database_Helper accessing_db = new Database_Helper(SmsReceiver.this);

それはファイル SmsReceiver.java にあります。私がやろうとしているのは、数字とテキストをデータベース テーブル Sms_repository に挿入することだけです。助けてください!

4

1 に答える 1

0

データベース クラスのコンテキストが必要です。onReceive パラメータでコンテキストを使用します。

Database_Helper accessing_db = new Database_Helper(context);
于 2013-05-06T21:50:08.610 に答える