未注册用于收听短信

时间:2012-12-28 11:13:51

标签: android

现在我要使用smsReceiver类。尽管应用程序在模拟器中运行顺畅,但它在真实设备上显示错误。该应用程序可以安装在两者中,但是当它进入真实设备时,它会显示一条像“短信听众已经联合国注册”的加密消息

这是我的SMSReceiver类:

  public void onReceive(Context context, Intent intent) {
this.mcontext = context;
if (intent.getAction().equals(VoiceofText.ACTION)) {
    Bundle bundle = intent.getExtras();
if (bundle != null) {
 readSMS(bundle);
}
 }
}

public void readSMS(Bundle bundle) {
SmsMessage[] msgs = null;

try {

 Object[] pdus = (Object[]) bundle.get("pdus");

 if (pdus != null) {

   msgs = new SmsMessage[pdus.length];

   String smsBodyStr = null;
   String phoneNoStr = null;

   for (int k = 0; k < msgs.length; k++) {
    msgs[k] = SmsMessage.createFromPdu((byte[]) pdus[k]);

    smsBodyStr = msgs[k].getMessageBody().trim();

    phoneNoStr = msgs[k].getOriginatingAddress().trim();

    speakSMS(smsBodyStr, phoneNoStr);
      }
}
} catch (Exception exe) {
    exe.printStackTrace();
}
} 

这是我使用的清单文件:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="a.b.c"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="8" />

 <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

     <receiver android:name=".SmsReceiver">  
         </receiver>      

    <activity
        android:label="@string/app_name"
        android:name=".SMSText" >

                    <intent-filter >
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity>


 </application>

<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>

   </manifest>

这里我在下一个名为SMSText.java的类中指定Toast消息

public void registerSMS() {
receiver = new SMSReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(ACTION);
registerReceiver(receiver, filter);

Toast.makeText(getApplicationContext(), "registered for incomming sms",
            Toast.LENGTH_LONG).show();
}


public void unregisterSMS() {
try {
unregisterReceiver(receiver);
Toast.makeText(getApplicationContext(),"unregistered for listening sms",  
                 Toast.LENGTH_LONG).show();
} 
catch (Exception exe) {Toast.makeText(getApplicationContext(),
                "sms listener is already unregistered",  
                        Toast.LENGTH_LONG).show();
 }
}

0 个答案:

没有答案
相关问题