1

ここでメッセージを読んで乾杯しようとしていますが、拡張する別のクラスがあるさまざまな例を見てきましたが、BroadcastReceiverこのクラスを開始する方法については言及していません (startactivity() などを使用しますか)。O'Reilly のクックブックからのリンクからダウンロードしたコードを投稿しました。ddms から SMS を送信しようとしましたが、トースト メッセージが表示されません。BroadcastReceiverを初めて使用するので、助けていただければ幸いです。

招待SMSreciever.java

package com.SMS;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.sax.StartElementListener;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;

public class invitationSMSreciever extends BroadcastReceiver {

    final String TAG = "BombDefusalApp";

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        SmsMessage[] msgs = null;
        String message = "";
        if (bundle != null) {
            Object[] pdus = (Object[]) bundle.get("pdus");
            msgs = new SmsMessage[pdus.length];
            for (int i = 0; i < msgs.length; i++) {
                msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                message = msgs[i].getMessageBody();
                Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
                if (msgs[i].getMessageBody().equalsIgnoreCase("Invite")) {
                    // Intent myIntent = new Intent(MainMenu.this,
                    // com.bombdefusal.ReceivedSMSActivity.class);
                    Intent myIntent = new Intent();
                    myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    myIntent.setAction("com.example.helloandroid.INVITE");
                    context.startActivity(myIntent);
                }
            }
        }
    }
}

メインメニュー

package com.SMS;

import com.SMS.R;

import android.app.Activity;
import android.os.Bundle;

public class MainMenu extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

受信した SMS アクティビティ

package com.SMS;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;

import com.SMS.R;

public class ReceivedSMSActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        startService(new Intent ("com.android.PLAY"));
        setContentView(R.layout.invite);
    }
    public boolean onKeyDown(int keyCode, KeyEvent service) {
        stopService(new Intent("com.bombdefusal.START_AUDIO_SERVICE"));
        finish();
        return true;
    }
}

マニフェスト

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.SMS" android:versionCode="1" android:versionName="1.0">
    <uses-permission android:name="android.permission.RECEIVE_SMS"/>
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MainMenu" android:label="@string/app_name">
            <intent-filter>
                <action android:name="com.SMS" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name="com.SMS.ReceivedSMSActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="com.example.helloandroid.INVITE"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>

        <receiver android:name="com.SMS.invitationSMSreciever"
                  android:enabled="true">
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>          
        </receiver>
    </application>
</manifest> 
4

1 に答える 1

1

これを行うには、次の 2 つのオプションがあります。

  1. ブロードキャスト レシーバーを AndroidManifest ファイルに静的に登録します。したがって、自動的に呼び出されます。
  2. メソッドを使用して、ブロードキャストレシーバーをコードに動的に登録しますregisterReceiver()。この場合、このメソッドはunregisterReceiver()、受信者の登録を解除する場所と組み合わせる必要があります。

通常、ブロードキャスト レシーバーが別のクラスとして実装されている場合、通常は AndroidManifest ファイルに静的に登録されます。あなたの場合、次の行をファイルに追加するだけでよいと思います。

<receiver android:name=".invitationSMSreciever" android:exported="true" > 
  <intent-filter android:priority="1000"> 
    <action android:name="android.provider.Telephony.SMS_RECEIVED" />
  </intent-filter> 
</receiver>
于 2012-11-04T12:00:50.353 に答える