받은 SMS를 저장하기 위해 일부 코드를 추가 할 때까지 이전에 작동했던 다음 코드를 사용했습니다.메시지를받을 때 응용 프로그램이 작동을 멈 춥니 다.
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;
}
모르겠어요 경우 오류 와라.
logcat 추적 후 cursorToMessage 메서드 – Pragnani
이 코드를 주석으로 작성하십시오. Toast.makeText (context, body, Toast.LENGTH_LONG) .show(); 배경 서비스를 사용하고 있기 때문에 백그라운드 프로세스에서 Ui와 통신 할 수 없습니다. –
적절한 사용 권한을 추가하고 있습니까? –