广播活动之间的发送/接收

时间:2011-02-09 18:41:05

标签: android

Android新手并尝试在两个活动之间实现广播通信。我在书籍,网站上看了很多例子,并且在这里有几个主题,但我仍然遗漏了一些东西。大多数样本都有一个活动,它扩展自BroadcastActivity,我相信这不是我的应用所需要的。我把一个直接的样本放在一起进行测试。

我错过了什么?整个代码如下。

感谢您的帮助。

package com.dialogtest.dt;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class DialogActivity extends Activity {

        public static final String DTAG = "ADebug";

        public static String strDia = "declr";

        public static String BroadMess = "com.dialogtest.dt.Restart";
        public static TextView tv;
    }

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends DialogActivity {

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

        strDia = "One";

        tv = (TextView) findViewById(R.id.TextView_Main);
        final Button diaButton = (Button) findViewById(R.id.Button_A);

        diaButton.setOnClickListener(new View.OnClickListener() {           
           @Override
           public void onClick(View v) {

              startActivity(new Intent(MainActivity.this, TestActivity.class));                                 
        }   
     });                                    
    }

    BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {         
            tv.setText("SUCCESS - RECEIVED!");
            Log.e(DTAG, "in Main - RECEIVED BROADCAST - str: " + strDia);
        }
    };

    @Override
    protected void onResume() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(BroadMess);
        registerReceiver(receiver, filter);

        super.onResume();
    }

    @Override
    protected void onPause() {
        unregisterReceiver(receiver);

        super.onPause();
    }        
}

public class TestActivity extends DialogActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        
        setContentView(R.layout.two_dialog);

       final Button dB = (Button) findViewById(R.id.ButtonDone);       
       dB.setOnClickListener(new View.OnClickListener() {           
       @Override
       public void onClick(View v) {
           finish();
       }    
     });     
    }

    @Override
    public void onStart() {
        super.onStart();

        EditText dET = (EditText) findViewById(R.id.EditText01);       
        dET.setText(strDia);
    }

    @Override
    public void onPause() {
        super.onPause();

        EditText dET = (EditText) findViewById(R.id.EditText01);
        strDia = dET.getText().toString();

        Intent broadcast = new Intent();
        broadcast.setAction(BroadMess);
        sendBroadcast(broadcast);

        Log.e(DTAG, "in Test onPause Sending Broadcast - strDia: " + strDia);
    }

}

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

    <activity android:name="DialogActivity"></activity>
<activity android:name="TestActivity" android:theme="@android:style/Theme.Dialog"></activity>
</application>
    <uses-sdk android:minSdkVersion="3" />
</manifest>

1 个答案:

答案 0 :(得分:4)

使用广播在两个活动之间进行通信是没有意义的。一次只能激活其中一个活动。例如,使用它在活动和服务之间进行通信是有意义的。

我不知道你想要传达什么,但通常你可以使用intent extras将信息传递给下一个Activity。即,使用putExtra

(java.lang.String,android.os.Bundle)