枝..
我正在创建一个应用程序,我在其中检测SIM是否已更改..?
在安装我的应用程序时,我将当前的SIM序列号存储在共享首选项中。
在启动时,我已经注册了广播接收器。在onReceive()
方法中,我正在访问当前的SIM序列号,并在安装时将其与存储的序列号进行比较,
以下是我的接收者代码:
package com.secuirity.sms;
import org.apache.harmony.xnet.provider.jsse.GMailSender;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.telephony.SmsManager;
import android.telephony.TelephonyManager;
import android.util.Log;
public class BootUpReciever extends BroadcastReceiver{
Context context;
String email;
String currentSimSerial;
SharedPreferences settings;
SmsManager smsMgr = SmsManager.getDefault();
public static final String PREFS_NAME = "MyPrefsFile";
@Override
public void onReceive(Context context, Intent intent) {
settings = context.getSharedPreferences(PREFS_NAME, 0);
String storedSimSerial = settings.getString("storedSimSerial", null);
Log.d("Stored Sim Serial::",storedSimSerial);
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
currentSimSerial = tm.getSimSerialNumber();
Log.d("Current Sim Serial","::"+currentSimSerial);
String trustedNum = settings.getString("cellno", null);
email = settings.getString("email", null);
if(currentSimSerial == storedSimSerial){
}else{
Log.d("Sim changed","!!!");
new GmailAsync().execute("");
String sms = "Sim card has changed, " +
"Sim Serial Number of this card is\n"+currentSimSerial+
"Network Operator"+tm.getNetworkOperatorName();
smsMgr.sendTextMessage(trustedNum, null,sms, null, null);
}
Intent sms = new Intent(context, SMSReceiver.class);
sms.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(sms);
Intent netAvailability = new Intent(context, CheckingNetworkAvailability.class);
netAvailability.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(netAvailability);
}
public class GmailAsync extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
String mail_body = "Sim serial number is "+currentSimSerial;
String subject = "Your Sim has changed!!!";
GMailSender sender = new GMailSender("securemob.viasms@gmail.com", "smsfun890");
try {
sender.sendMail(subject,
mail_body+"\nThanks for using Mobile Security App",
"securemob.viasms@gmail.com",
email,null);
} catch (Exception e) {
Log.e("SendMail", e.getMessage(), e);
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
Log.d("Mail","Sent");
}
}
}
但它不起作用...... :( :( :( 我有以下权限READ_PHONE_STATE“和”RECEIVE_BOOT_COMPLETED“
答案 0 :(得分:0)
我的代码中看不到任何错误,但我要检查的第一件事是你有权阅读Manifest中的SIM卡。
答案 1 :(得分:0)
Hie Guys我得到了BOOT UP接收器的解决方案.... 当Android移动启动时,此事件将广播到每个应用程序,但是与存储在SD卡上的应用程序相比,安装在内部存储器中的应用程序很快就会响应。 因此解决方案是
android:installLocation="internalOnly"
在清单文件中作为清单属性。