1

在这里,我试图阅读消息并为它敬酒我已经看到了各种示例,其中有一个单独的类扩展BroadcastReceiver但他们没有提到如何启动这个类(我们使用 startactivity() 还是其他东西)。我已经发布了我通过 O'Reilly 食谱中的链接下载的代码。我试过从 ddms 发短信,但它没有显示消息。感谢任何帮助,因为这是我第一次使用 BroadcastReceiver。

邀请短信reciever.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);
    }
}

收到短信活动

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

您有两种选择:

  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 回答