我已经使用了广播接收器并捕获了短信..我想只将短信从我的应用程序发送到我的应用程序安装的另一个移动设备..我使用优先级但它没有工作..我希望移动设备短信defualt收件箱将无法获取短信..只有我的应用程序收件箱将获得短信..我已经为短信收件箱创建了自己的数据库...该怎么办?
//my sms receiver 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();
DatabaseHandler db = new DatabaseHandler(context);
db.addSMS(new IncomingSMS(str,str2));
}
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);*/
}
}
}
答案 0 :(得分:1)
停止广播使用
this.abortBroadcast();
在最后阻止之后