2012-09-05 36 views
1

我有這個程序會嘗試監控收到的SMS消息。但是,當我嘗試在模擬器中運行我的程序並嘗試發送短信時,它可以正常工作。但是當我將我的程序安裝到手機中時,它不起作用。在Android中監控收到的SMS消息

問題是什麼?

我也想在後端運行程序。怎麼做?

順便說一句,下面是這個示例應用程序的整個代碼。

感謝

RJ


import java.io.BufferedWriter; 
import java.io.FileWriter; 

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.telephony.SmsMessage; 
import android.util.Log; 

public class MySMSMonitor extends BroadcastReceiver 
{ 
    private static final String ACTION = "android.provider.Telephony.SMS_RECEIVED"; 

    @Override 
    public void onReceive(Context context, Intent intent) 
    { 
     if(intent!=null && 
       intent.getAction()!=null && 
       ACTION.compareToIgnoreCase(intent.getAction())==0) 
     { 
      Object[]pduArray= (Object[]) intent.getExtras().get("pdus"); 
      SmsMessage[] messages = new SmsMessage[pduArray.length]; 

      for (int i = 0; i<pduArray.length; i++) { 
       messages[i] = SmsMessage.createFromPdu ((byte[])pduArray [i]); 
      } 
      Log.d("MySMSMonitor","SMS Message Received."); 
     } 
    } 
} 

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
     package="spy.Frandy.com" 
     android:versionCode="1" 
     android:versionName="1.0"> 
    <uses-sdk android:minSdkVersion="6" /> 
    <uses-permission android:name="android.permission.SEND_SMS" /> 
    <uses-permission android:name="android.permission.RECEIVE_SMS" /> 

    <application android:icon="@drawable/icon" android:label="@string/app_name"> 
     <activity android:name=".TelephonyDemo" 
        android:label="@string/app_name"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

     <receiver android:name=".MySMSMonitor"> <intent-filter> 
      <action android:name="android.provider.Telephony.SMS_RECEIVED"/> </intent-filter> 
     </receiver> 

    </application> 
</manifest> 

import android.app.Activity; 
import android.os.Bundle; 
import android.telephony.SmsManager; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

public class TelephonyDemo extends Activity 
{ 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.main); 
     Button sendBtn = (Button)findViewById(R.id.sendSmsBtn); 

     sendBtn.setOnClickListener(new OnClickListener(){ 
      @Override public void onClick(View view) { 
      EditText addrTxt = (EditText)TelephonyDemo.this.findViewById(R.id.addrEditText); 

      EditText msgTxt = (EditText)TelephonyDemo.this.findViewById(R.id.msgEditText); 
      try { 
       sendSmsMessage(
         addrTxt.getText().toString(),msgTxt.getText().toString()); 
       Toast.makeText(TelephonyDemo.this, "SMS Sent", 
         Toast.LENGTH_LONG).show(); 
      } catch (Exception e) { 
       Toast.makeText(TelephonyDemo.this, "Failed to send SMS", Toast.LENGTH_LONG).show(); 
       e.printStackTrace(); 
      } 
     }}); 
    } 

    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 
    } 

    private void sendSmsMessage(String address,String message)throws Exception { 
     SmsManager smsMgr = SmsManager.getDefault(); 
     smsMgr.sendTextMessage(address, null, message, null, null); 
    } 
} 
+0

說不工作不會幫助,引腳指出錯誤。這是一個崩潰?或一些功能不工作 – nandeesh

+0

它不會暗戀.. onReceive()不被稱爲 – user1120260

+0

嘗試得到一個剛剛在行後面的敬酒:公共無效onReceive(上下文上下文,意圖意圖) {,之前你把你的條件。 – Hiral

回答

1

我只想堅持你使用ContentObserver註冊content://sms URI和使用

cusor.getString(cusor.getColumnIndex("type")); // 1 = SMS Received 
               // 2 = SMS Sent 

而且你可以很容易地從光標,你獲取所需的數據獲取短信typeHere是一個相同的博客。

+0

謝謝我會嘗試這一個... – user1120260

+0

雖然這個答案對於監測短信(過去/現在/將來)來說是理想的,但谷歌已經間接地指出,查詢ContentProvider的內容:/sms'可能會破壞較新的設備,因爲它不是SDK的一部分。現在顯然我懷疑任何製造或短信應用程序會偏離向後兼容,但你永遠不知道。我仍然擔心讓廣播接收器工作,至少在未來的項目中。 http://stackoverflow.com/a/1976557/1443717 – StrikeForceZero

1

我打算用小細節假設你已經提供了理由它適用於模擬器而不是您的實際電話;可能是另一個相互衝突的應用程序在廣播到您的應用程序之前攔截消息。

嘗試在你的應用程序中添加android:priority="1000"到接收機的你的意圖過濾器清單 (一千是什麼號碼,你覺得有必要,可能會更高,如果需要的話)上

<intent-filter android:priority="1000" > 
      <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
</intent-filter> 

更多信息 - intent-filter priority

+0

仍然不能正常工作... – user1120260

+0

你用什麼來「發送」和短信到手機?你是否通過股票消息應用程序獲取了SMS的通知? – StrikeForceZero

0

試試這個:

<receiver android:name=".MySMSMonitor" 
       android:permission="android.permission.BROADCAST_SMS"> 

     <intent-filter android:priority="2"> 
     <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
     </intent-filter> 
    </receiver> 
+0

不能正常工作...... – user1120260

相關問題