1

私は最近これに戸惑いました.2.3.5より前では、これはうまく機能しているようです(私の同僚のデバイスでは)。ただし、私の場合はトリガーされません。

コードを削除して、何が起こっているかを確認するための非常に単純なテスト アプリケーションを作成しました。基本的に、adb/logcat は BroadcastReveiver の登録が行われていることを示しているように見えますが、onReceive コードがトリガーされるようには見えません。

これが私が行った簡単なコードです:

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

<uses-sdk android:minSdkVersion="9" />

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

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <receiver android:name=".mysmstestcall">
    <intent-filter>
        <action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
    </intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
</manifest>

それで:

package com.broadcasttech.testsmsreceive;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;

public class TestSMSReceiveActivity extends Activity {
private BroadcastReceiver receiver;
private static final String TAG = "TestSMSApp";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Log.i(TAG, " App has started up");
    IntentFilter filter = new IntentFilter();
    filter.addAction("android.provider.Telephony.SMS_RECEIVED");
    Log.i(TAG, " Filter SMS_RECEIVED has been added");
    //Extends BroadcastReceiver
    receiver = new mysmstestcall();
    registerReceiver(receiver,filter);
    Log.i(TAG, " registerReceiver sorted");
}

//Also, to save headaches later
@Override
protected void onDestroy() {
  Log.i(TAG, " unregistering Receiver");
  unregisterReceiver(receiver);
  Log.i(TAG, " done");
}
}

そして最後に

package com.broadcasttech.testsmsreceive;

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

public class mysmstestcall extends BroadcastReceiver {

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

@Override
public void onReceive(Context context, Intent intent) {
     Log.i(TAG, "Intent recieved: " + intent.getAction());
     if (intent.getAction() == SMS_RECEIVED) {
         //any action you want here..
         Log.i(TAG, "SMS received has triggered");
     }
}
}

したがって、BroadcastReceiver がトリガーされたときにログに記録して通知するだけの非常に単純なアプリですが、まったく起動しません。

私はさまざまなチュートリアルをチェックし、IceCreamSandwich が異なることを知っているのでチェックしましたが、それらの修正も取り入れようとしましたが、これも違いはありません。

前もって感謝します!

4

4 に答える 4

2

主な問題は、「mysmstestcall」の次の行が間違っていることです。

if (intent.getAction() == SMS_RECEIVED)

これに変更する必要があります:

if (intent.getAction().equals(SMS_RECEIVED)) 
于 2012-05-17T16:59:04.227 に答える
0

私はこれを一度持っています。ネットワークの問題である可能性があります。これらは私の質問と答えでした。送信インテントを追加して、結果コードをキャッチできます。私の場合はでしたRESULT_ERROR_GENERIC_FAILURE。私は何ヶ月も他の解決策を見つけようとしましたが、運がなかったので、私はそれを受け入れました:-(

于 2012-07-22T01:22:26.407 に答える
0

レシーバーが2つある理由はありますか?プログラムリスナーXML リスナーがあります。

プログラマティック:

filter.addAction("android.provider.Telephony.SMS_RECEIVED");
Log.i(TAG, " Filter SMS_RECEIVED has been added");
//Extends BroadcastReceiver
receiver = new mysmstestcall();

XML リスナー:

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

本当にこの2つが欲しいですか?2 つある場合は、同じブロードキャストを 2 回受信することになります...

私が見る別の問題については、この行です

if (intent.getAction() == SMS_RECEIVED)

文字列を比較するとき、次のように比較しないでください。代わりに、次のようになります。

if (intent.getAction().equalsIgnoreCase(SMS_RECEIVED))
于 2012-01-10T13:39:59.167 に答える
0
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;

public class SmsBroadcastReceiver extends BroadcastReceiver{
    public static final String SMS_BUNDLE = "pdus";
    @Override
    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]);

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

                smsMessageStr += "SMS From: " + address + "\n";
                smsMessageStr += smsBody + "\n";
            }
            Toast.makeText(context, "A new message is added to the SMS List!!!\n"+smsMessageStr, Toast.LENGTH_SHORT).show();

            //this will update the UI with message
            InboxMain inst = InboxMain.instance();
            inst.updateList(smsMessageStr);

    }

}
}


manifest::
<receiver android:name=".SmsBroadcastReceiver" android:permission="android.permission.BROADCAST_SMS" android:exported="true">
    <intent-filter android:priority="2147483647" >
        <action android:name="android.provider.Telephony.SMS_RECEIVED" />
    </intent-filter>
 </receiver>

permission::
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.WRITE_SMS" />
    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
于 2015-04-16T07:41:36.017 に答える