2013-04-10 73 views
0

我有以下代碼,直到我添加了一些代碼來保存收到的短消息。當收到消息時,應用程序崩潰

package com.example.smsTest; 

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.telephony.SmsMessage; 
import android.widget.Toast; 

public class SmsReceiver extends BroadcastReceiver { 
    private SQLiteAdapter mySQLiteAdapter; 
@Override 
public void onReceive(Context context, Intent intent) { 
    Message message = null; 

     Bundle extras = intent.getExtras(); 
     if (extras == null) 
     return; 

     Object[] pdus = (Object[]) extras.get("pdus"); 
     for (int i = 0; i < pdus.length; i++) { 
      SmsMessage SMessage = SmsMessage.createFromPdu((byte[]) pdus[i]); 
      String sender = SMessage.getOriginatingAddress(); 
      String body = SMessage.getMessageBody().toString(); 

      message = mySQLiteAdapter.createMessage(body); 

      // A custom Intent that will used as another Broadcast 
     Intent in = new Intent("SmsMessage.intent.MAIN"). 
     putExtra("get_msg", sender+":"+body); 

     // To display a Toast whenever there is an SMS. 
     Toast.makeText(context,body,Toast.LENGTH_LONG).show(); 

     //You can place your check conditions here(on the SMS or the sender)    
     //and then send another broadcast 
     context.sendBroadcast(in); 

     // This is used to abort the broadcast and can be used to silently 
     // process incoming message and prevent it from further being 
     // broadcasted. Avoid this, as this is not the way to program an app. 
     this.abortBroadcast(); 
     } 
    } 
} 

,我認爲原因加入這個崩潰的代碼如下:

private SQLiteAdapter mySQLiteAdapter; 
Message message = null; 
message = mySQLiteAdapter.createMessage(body); 

這裏的CreateMessage函數的代碼:

public Message createMessage(String message) { 
    String[] columns = new String[]{KEY_ID, KEY_CONTENT}; 

    ContentValues values = new ContentValues(); 
    values.put(KEY_CONTENT, message); 
    long insertId = sqLiteDatabase.insert(MYDATABASE_TABLE, null, 
    values); 
    Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, 
      columns, KEY_ID + " = " + insertId, null, 
    null, null, null); 
    cursor.moveToFirst(); 
    Message newMessage = cursorToMessage(cursor); 
    cursor.close(); 
    return newMessage; 
} 

我不知道在哪裏的錯誤來自。

+3

發佈logcat跟蹤和cursorToMessage方法 – Pragnani 2013-04-10 07:12:04

+0

請評論此代碼Toast.makeText(context,body,Toast.LENGTH_LONG).show(); ,因爲您正在使用後臺服務,您無法在後臺進程中與UI進行通信 – 2013-04-10 07:12:53

+0

您是否添加了適當的權限? – 2013-04-10 07:13:22

回答

2

看起來像你沒有實例化mySQLiteAdapter。您必須在訪問它之前實例化。

+0

謝謝。但在添加所需的代碼後,如下所示:mySQLiteAdapter = new SQLiteAdapter(SmsReceiver); mySQLiteAdapter.openToRead();'。錯誤是「SmsReceiver無法解析爲變量」。 – jaypabs 2013-04-10 07:19:00

+0

嘗試SQLiteAdapter(SmsReceiver.this)或SQLiteAdapter(YourActivity) – BobTheBuilder 2013-04-10 07:20:28

+1

@jaypabs在構造函數mySQLiteAdapter = new SQLiteAdapter(context)中使用上下文; – Pragnani 2013-04-10 07:21:34

0

請註釋代碼的這條線,

Toast.makeText(context,body,Toast.LENGTH_LONG).show(); 

我認爲這會工作,我也面臨着後臺服務UI操作同樣的問題,請commnent代碼,或Log.d替換()登錄信息

相關問題