2012-12-31 93 views
1

我做在SIM跟蹤,但無法一個應用獲取引起辛跟蹤Android中

這裏是主要的活動

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(); 
      } 
     } 
    } 
} 

Reciever

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

太給出這些權限<使用的許可機器人:名稱=「android.permission.READ_PHONE_STATE」 /> <使用的許可機器人:名稱=「android.permission.RECEIVE_BOOT_COMPLETED」 /> <使用的許可機器人:名稱=「android.permission.SEND_SMS」/> achu

+0

爲什麼無法獲得結果?你面臨什麼錯誤? – RobinHood

+0

沒有錯誤,但重啓後無法使用 – achu

回答

1

我發現類似的問題,當工作在同一類型的項目。

我也無法跟蹤設備重啓後的SIM卡。我在這裏發現的問題是,我在設備重啓後立即調用sim追蹤。但系統需要15到20秒才能加載資源。該設備重新啓動後,SIM卡並沒有立即啓動,因此我的接收器無法跟蹤SIM卡。

因此,我在設備重新啓動後延遲了20秒的模擬跟蹤。嘗試延遲模擬跟蹤,並檢查它是否工作。

編輯 - 接收機應該是這樣的,

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); 
    } 
} 

,並在清單文件,替換該代碼,

<receiver android:name=".SimDataReciever" 
     android:enabled="true" 
     android:exported="false"> 
     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED"/> 
     </intent-filter> 
    </receiver> 

現在,檢查是否郵件中顯示logcat或不。

+0

仍然沒有迴應 – achu

+0

你可以發佈更新後的代碼,請刪除問題中不必要的代碼。 –

+0

更新了代碼.... – achu