我正在使用SIM卡跟踪但无法获得结果的应用
这是主要活动
public class MainActivity extends Activity {
String FILENAME = "old_file.txt";
int simstatus;
String simNo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TelephonyManager tManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
if (simstatus != TelephonyManager.SIM_STATE_ABSENT) {
System.out.println("--------SIM Present:" + simstatus);
simNo = tManager.getSimSerialNumber();
FileOutputStream fos;
try {
fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(simNo.getBytes());
System.out.println("---------Data written to files is:"
+ simNo);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
收件人
public class SimDataReciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
System.out.println("Reciever Started");
Intent CompareSimServiceIntent = new Intent(context,demo.class);
context.startService(CompareSimServiceIntent);
}
}
}
和服务..
String FILENAME = "old_file.txt";
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
public int onStartCommand(Intent intent, int flags, final int startId) {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
//run your service
// Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
TelephonyManager tManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
try {
FileInputStream fis = openFileInput(FILENAME);
InputStreamReader in = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(in);
String data = br.readLine();
System.out.println("---Data Read From File is:" + data);
String newsiminfo = tManager.getSimSerialNumber();
System.out.println("---New SIM no is:" + newsiminfo);
if (data.equals(tManager.getSimSerialNumber())) {
System.out.println("------Old sim Present:");
// Toast.makeText(this, "Old SIM", Toast.LENGTH_LONG).show();
} else {
// Toast.makeText(this, "New SIM", Toast.LENGTH_LONG).show();
SmsManager smsMngr = SmsManager.getDefault();
String destinationaddress = "8281306132";
String scAddress = null;
String text = "New Sim Is Inserted In Your Device";
PendingIntent sentIntent = null;
PendingIntent deliveryIntent = null;
smsMngr.sendTextMessage(destinationaddress, scAddress, text,
sentIntent, deliveryIntent);
System.out.println("-----SMS Send");
}
} catch (Exception e) {
}
}
}, 1*60*1000);
return startId;
}
}
请帮我找到解决方案......
答案 0 :(得分:1)
在处理同类项目时,我发现了类似的问题。
设备重启后我也无法跟踪SIM卡。我在这里发现的问题是我在设备重启后立即调用了SIM卡跟踪。但系统需要15到20秒才能加载资源。设备重启后,SIM卡无法正常启动,因此我的接收器无法跟踪SIM卡。
因此,我在设备重启后将SIM卡跟踪延迟了20秒。尝试延迟SIM卡跟踪并检查它是否有效。
修改 - 强> 您的接收器应该是这样的,
public class SimDataReciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("Reciever Started");
Log.d("BOOT COMPLETE","Receiver Called");
Intent CompareSimServiceIntent = new Intent(context,demo.class);
context.startService(CompareSimServiceIntent);
}
}
并在Manifest文件中,用此代码替换您的代码,
<receiver android:name=".SimDataReciever"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
现在,检查消息是否显示在logcat中。