簡體   English   中英

SMS_RECEIVED沒有在冰淇淋三明治上工作?

[英]SMS_RECEIVED not working on Ice Cream Sandwich?

我正在嘗試使用android.provider.Telephony.SMS_RECEIVED來捕獲傳入的短信。

我構建了一個簡單的應用程序,它適用於2.x,但是當我在我的4.0模擬器或設備上試用它時,它不起作用。

有任何想法嗎?

表現:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.giggsey.MyFirstApp" android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name">


    <receiver android:name=".MyFirstApp">
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
        </intent-filter>
    </receiver>
</application>
<uses-sdk android:minSdkVersion="9" />


<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
</manifest>

MyFirstApp.java

public class MyFirstApp extends BroadcastReceiver {

    private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
    private static final String TAG = "MyFirstApp";

    @Override
    public void onReceive(Context context, Intent intent) {
         Log.i(TAG, "Intent recieved: " + intent.getAction());
    }
}

確保您實際上是在活動或服務中創建和注冊接收器,否則它將不會被調用(我相信)。

一個非常簡單的例子可能是:

public class MyActivity extends Activity {

    private BroadcastReceiver receiver;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        IntentFilter filter = new IntentFilter();
        filter.addAction("android.provider.Telephony.SMS_RECEIVED");
        //Extends BroadcastReceiver
        receiver = new MyFirstApp();
        registerReceiver(receiver,filter);
    }


   //Also, to save headaches later
   @Override
   protected void onDestroy() {
        unregisterReceiver(receiver);
   }
}

我不能保證這會起作用,但我相信它會解決一些問題。 如果您對某些內容有任何疑問,請在評論中提問。 我相信你是正確的說它甚至沒有被調用,因為你的接收器沒有注冊任何東西。 如果您希望它在后台運行,請考慮使用服務。 我真的希望這對你的努力有所幫助,祝你好運!

您只需要為接收器導出值true。

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

如果你在背景中嘗試“捕獲”,你可以看到這篇文章

"android.provider.Telephony.SMS_RECEIVED"Service中運行良好 否則只有在活動生命周期中你才能“抓住”它

我認為您的錯誤是您在包名中使用了類名。

在您的清單中,您在<receiver android:name=".MyFirstApp">編寫了package="com.giggsey.MyFirstApp"以及<receiver android:name=".MyFirstApp"> 這意味着你的接收者的全名是com.giggsey.MyFirstApp.MyFirstApp ,但我相信它只是com.giggsey.MyFirstApp

交易所com.giggsey.MyFirstApp在你的清單與com.giggsey是要高度重視的工作,如果我猜的沒錯。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
         package="com.giggsey" android:versionCode="1" android:versionName="1.0">
[...]

而且這個:

package com.giggsey;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class MyFirstApp extends BroadcastReceiver {
    private static final String TAG = "MyFirstApp";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(TAG, "Intent recieved: " + intent.getAction());
    }
}

這可能會幫助你......在brodcast接收器類中

 public static final String SMS_BUNDLE = "pdus";


public void onReceive(Context context, Intent intent)
{

     Bundle intentExtras = intent.getExtras();
        if (intentExtras != null) {
            Object[] sms = (Object[]) intentExtras.get(SMS_BUNDLE);
            String smsMessageStr = "";
            for (int i = 0; i < sms.length; ++i) {
                SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) sms[i]);

                smsBody = smsMessage.getMessageBody().toString();
                address = smsMessage.getOriginatingAddress();

                smsMessageStr += "SMS From: " + address + "\n";
                smsMessageStr += smsBody + "\n";
            }
            Toast.makeText(context, smsMessageStr, Toast.LENGTH_SHORT).show();
    }

在接收請嘗試添加以下行

private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";

if (intent.getAction() == SMS_RECEIVED) {
    //any action you want here..
    Toast.makeText(MyClass.this, "SMS RECEIVED",Toast.LENGTH_LONG).show();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM