简体   繁体   中英

Constructor undefined in a BroadCastReceiver class

I am trying to insert data from my remote server into my local database when a gcm message is received from my servers. I have created a database controller class and broadcastreceiver class.

Database Controller class

public class DBController  extends SQLiteOpenHelper {

    public DBController(Context applicationcontext) {
        super(applicationcontext, "user.db", null, 1);
    }
    //Creates Table
    @Override
    public void onCreate(SQLiteDatabase database) {
        String query;
        query = "CREATE TABLE users ( userId INTEGER, userName TEXT)";
        database.execSQL(query);
    }
    @Override
    public void onUpgrade(SQLiteDatabase database, int version_old, int current_version) {
        String query;
        query = "DROP TABLE IF EXISTS users";
        database.execSQL(query);
        onCreate(database);
    }

    /**
     * Inserts User into SQLite DB
     * @param queryValues
     */
    public void insertUser(HashMap<String, String> queryValues) {
        SQLiteDatabase database = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("userId", queryValues.get("userId"));
        values.put("userName", queryValues.get("userName"));
        database.insert("users", null, values);
        database.close();
    }

    /**
     * Get list of Users from SQLite DB as Array List
     * @return
     */
    public ArrayList<HashMap<String, String>> getAllUsers() {
        ArrayList<HashMap<String, String>> usersList;
        usersList = new ArrayList<HashMap<String, String>>();
        String selectQuery = "SELECT  * FROM users";
        SQLiteDatabase database = this.getWritableDatabase();
        Cursor cursor = database.rawQuery(selectQuery, null);
        if (cursor.moveToFirst()) {
            do {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("userId", cursor.getString(0));
                map.put("userName", cursor.getString(1));
                usersList.add(map);
            } while (cursor.moveToNext());
        }
        database.close();
        return usersList;
    }

}

GCMBroadcastReceiver.java

public class GCMBroadcastReceiver extends BroadcastReceiver {
    DBController controller = new DBController(this);
    HashMap<String, String> queryValues; 

     @Override
        public void onReceive(Context context, Intent intent) {
             ...
        }
}

Now the problem is when my Database Controller class (DBController.java) is called in the GCMBroadcastReceiver.java class it gives an error message below. Please is there something i am doing wrong?

Description Resource Path Location Type The constructor DBController(GCMBroadcastReceiver) is undefined GCMBroadcastReceiver.java /GCMtest/src/com/gcm/gcmtest line 14 Java Problem

In GCMBroadcastReceiver , you have a line with new DBController(this) in it. A BroadcastReceiver is not a Context , and so there is no valid constructor. Use the Context passed into onReceive() . This will mean delaying initializing that field until onReceive() .

In your GCMBroadcastReceiver class, you initialize DBController(this) . Try initializing your DBController inside of your onReceive() method. You can use the context from the method parameter so it looks like this: controller = new DBController(context);

Initialize your DBController in onReceive() method because the context will be passed to it and you can nuse that context to initialize your DBController.

For ex. like this..

public class GCMBroadcastReceiver extends BroadcastReceiver {
DBController controller ;
HashMap<String, String> queryValues; 

 @Override
    public void onReceive(Context context, Intent intent) {
         controller =new DBController(context);
    }

}

I hope this would help you.

Once it is initialized you can use its instance anywhere in this class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM