0

喂..

我正在制作一个应用程序,我在其中检测 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"

4

2 回答 2

0

嗨,伙计们,我得到了 BOOT UP Receiver 的解决方案.... 当 android 手机启动时,这个事件会广播到每个应用程序,但是与存储在 SD 卡上的应用程序相比,安装在内部存储器中的应用程序响应速度很快。因此解决方案是

android:installLocation="internalOnly"

在清单文件中作为清单属性。

于 2012-12-04T10:07:23.243 回答
0

我看不出您的代码有任何问题,但我首先要检查的是您是否有权读取清单中设置的 SIM 卡。

于 2012-12-03T12:25:46.053 回答