每次更换SIM卡时,我都会尝试将短信广播到少数手机号码。不幸的是,当我在我的设备上试用它时,它不起作用。有人可以在我的以下代码中找出问题。所有onBoot权限都已添加。
public class SendSmsOnTheft extends BroadcastReceiver
{
static void sendSMS(String destinationAddress, String text, Context context)
{
Intent intent = new Intent(context, SendSmsOnTheft.class);
PendingIntent pendingintent = PendingIntent.getActivity(context, 0, intent, 0);
SmsManager.getDefault().sendTextMessage(destinationAddress, null, text, pendingintent, null);
}
public void onReceive(Context context, Intent intent)
{
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction()))
{
Log.d("VogueTools", "Got the Boot Event>>>");
TelephonyManager telephonymanager = (TelephonyManager)context.getSystemService("phone");
SharedPreferences sharedpreferences = context.getSharedPreferences("file", 0);
String number1 = sharedpreferences.getString("no1", "");
String number2 = sharedpreferences.getString("no2", "");
String number3 = sharedpreferences.getString("no3", "");
String number4 = sharedpreferences.getString("no4", "");
String extra = ("Mobile of IMEI NO: ")+
telephonymanager.getDeviceId()+
" is currently used" +
" by the SIM card service provider:" +
telephonymanager.getSimOperatorName().toString();
String simno = sharedpreferences.getString("simno", "");
String serialno = ((TelephonyManager)context.getSystemService("phone")).getSimSerialNumber();
if (number1.length() == 0)
{
System.exit(0);
}
if (simno.equals(serialno))
{
sendSMS(number1, extra, context);
sendSMS(number2, extra, context);
sendSMS(number3, extra, context);
sendSMS(number4, extra, context);
}
}
System.exit(0);
}
}
答案 0 :(得分:0)
我认为我们需要更多信息才能帮到您。无论如何,你有没有正确注册接收器?在申请时你一定是这样的:
<receiver
android:name=".SendSmsOnTheft"
android:enabled="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
有权限:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.SEND_SMS" />
答案 1 :(得分:0)
以下是我在我的应用中出于同样目的使用的代码:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.telephony.TelephonyManager;
import android.telephony.gsm.SmsManager;
import android.util.Log;
import android.widget.Toast;
public class ListeningToBoot extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
// TODO Auto-generated method stub
if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED"))
{
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
String urphone = tm.getLine1Number();
Log.v("Uphone","is "+urphone);
Toast.makeText(context, "MDN:"+urphone, Toast.LENGTH_SHORT).show();
SharedPreferences prefs = context.getSharedPreferences("MySimPreferences", Context.MODE_PRIVATE);
String mobile_No=prefs.getString("MDN", "un-known");
Log.v("from shared pref","MDN"+mobile_No);
String imei=prefs.getString("IMEI", "un-known");
Log.v("from shared pref","IMEI"+imei);
if(urphone.equals(mobile_No))
{
Log.v("MDN is same","shoudl do nothing");
Toast.makeText(context, "MDN no change", Toast.LENGTH_LONG).show();
}
else
{
Log.v("MDN is not same","send a message to someone");
SharedPreferences prefs_names = context.getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);
String number_to_send=prefs_names.getString("Moblie_Number", "un-known");
Toast.makeText(context, "MDN changed", Toast.LENGTH_LONG).show();
String message_to_send="Your mobile number chagned:\nNew Number: "+urphone+"\nIMEI: "+imei;
SmsManager.getDefault().sendTextMessage(number_to_send, null, message_to_send, null,null);
}
}
}
}
希望这有帮助。
编辑:确保您已在清单文件中提供了所有必需的权限。