0

收到消息时出现一个空白窗口...如何克服它..如何在后台进行???给出了我的接收、存储和数据库类...

package com.example.crypton;



import java.util.ArrayList;
import java.util.List;

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

public class DatabaseHandler extends SQLiteOpenHelper {

// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "contactsManager1";

// Contacts table name
private static final String TABLE_CONTACTS = "contacts";

// Contacts Table Columns names
private static final String KEY_NAME = "name";
private static final String KEY_PH_NO = "phone_number";

public DatabaseHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
            + KEY_NAME + " TEXT,"
            + KEY_PH_NO + " TEXT" + ")";
    db.execSQL(CREATE_CONTACTS_TABLE);
}

// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);

    // Create tables again
    onCreate(db);
}

/**
 * All CRUD(Create, Read, Update, Delete) Operations
 */

// Adding new contact
void addSMS(IncomingSMS sms) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_NAME, sms.getDesc()); // Contact Name
    values.put(KEY_PH_NO, sms.getphonenumber()); // Contact Phone

    // Inserting Row
    db.insert(TABLE_CONTACTS, null, values);
    db.close(); // Closing database connection
}

// Getting single contact
   /* Contact getContact(int id) {
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_ID,
            KEY_NAME, KEY_PH_NO }, KEY_ID + "=?",
            new String[] { String.valueOf(id) }, null, null, null, null);
    if (cursor != null)
        cursor.moveToFirst();

    Contact contact = new Contact(Integer.parseInt(cursor.getString(0)),
            cursor.getString(1), cursor.getString(2));
    // return contact
    return contact;
}*/

// Getting All Contacts
public ArrayList<IncomingSMS> getAllsms() {
    ArrayList<IncomingSMS> smsList = new ArrayList<IncomingSMS>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_CONTACTS;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            IncomingSMS sms = new IncomingSMS();
            sms.setDesc(cursor.getString(0));
            sms.setphonenumber(cursor.getString(1));
            // Adding contact to list
            smsList.add(sms);
        } while (cursor.moveToNext());
    }

    // return contact list
    return smsList;
}

// Updating single contact
public int updateContact(IncomingSMS sms) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_NAME, sms.getDesc());
    values.put(KEY_PH_NO, sms.getphonenumber());

    // updating row
    return db.update(TABLE_CONTACTS, values, KEY_NAME + " = ?",
            new String[] { String.valueOf(sms.getDesc()),String.valueOf(sms.getphonenumber()) });
}

// Deleting single contact
public void deleteSMS(IncomingSMS sms) {
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(TABLE_CONTACTS, KEY_NAME + " = ?",
            new String[] { String.valueOf(sms.getDesc()) });
    db.close();
}


// Getting contacts Count
public int getContactsCount() {
    String countQuery = "SELECT  * FROM " + TABLE_CONTACTS;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(countQuery, null);
    cursor.close();

    // return count
    return cursor.getCount();
}

}

//smsreceiver class
package com.example.crypton;

import java.util.ArrayList;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.PhoneLookup;
import android.telephony.SmsMessage;
import android.widget.Toast;



public class SmsReceiver extends BroadcastReceiver  {

ArrayList<String> your_array_list = new ArrayList<String>();
@Override
public void onReceive(Context context, Intent intent) 
{
    //---get the SMS message passed in---
    Bundle bundle = intent.getExtras();        
    SmsMessage[] msgs = null;
   String str = "";    
   String str2 = ""; 
    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]);                

            str2 +=  msgs[i].getOriginatingAddress();                     
           // str2 += " :";
            str += msgs[i].getMessageBody().toString();

        }


        Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(str2));
        Cursor c = context.getContentResolver().query(lookupUri, new String[]{ContactsContract.Data.DISPLAY_NAME},null,null,null);
        try {
            c.moveToFirst();
         String  displayName = c.getString(0);
         str2 = displayName;   

        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            c.close();
        }
        //---display the new SMS message---
        Toast.makeText(context, str, Toast.LENGTH_LONG).show();

     Intent i = new Intent(context, StoreMsg.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        i.putExtra("sms", str);
        i.putExtra("phonenumber",str2);
        context.startActivity(i);


    }   
}  

}

//storemsg class

package com.example.crypton;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class StoreMsg extends Activity {

String finalmessage;
String phonenumber;

public void onCreate(Bundle savedInstanceState){ super. onCreate (savedInstanceState);

Intent intent = getIntent();
 finalmessage = intent.getStringExtra("sms");
 phonenumber=intent.getStringExtra("phonenumber");
DatabaseHandler db = new DatabaseHandler(this);
db.addSMS(new IncomingSMS(finalmessage,phonenumber)); 
//Intent i = new Intent(StoreMsg.this,INBOX.class); 
//StoreMsg.this.startActivity(i);

}

}

4

1 回答 1

0

那是因为您一收到消息就开始了 StoreMsg 活动。将您在数据库中存储消息的代码移动到您的onReceive方法中。喜欢:

@Override
public void onReceive(Context context, Intent intent) 
{
   //Your code

   //And now the code you used on your activity
   DatabaseHandler db = new DatabaseHandler(this);
   db.addSMS(new IncomingSMS(finalmessage,phonenumber)); 
}

或者,如果您确实希望您的活动显示某些内容,请在其上设置视图:

setContentView(view)
于 2013-09-06T20:45:09.400 回答