2011-02-09 63 views
1

Android新手,試圖實現兩項活動之間的廣播通信。我在書籍,網站上看過很多例子,並且在這裏跟隨了幾個主題,但我仍然錯過了一些東西。大多數示例都有一個從BroadcastActivity擴展而來的Activity,我相信這不是我的應用程序所需要的。我把一個簡單的樣本放在一起測試。活動之間的廣播發送/接收

我在想什麼?整個代碼如下。

感謝您的幫助。

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> 

回答

4

使用廣播在兩個活動之間進行通信沒有意義。這些活動中只有一個會一次活動。例如,使用它在活動和服務之間進行通信確實有意義。

我不知道你在試圖溝通什麼,但一般情況下你可以使用intent extras來傳遞信息到下一個Activity。即,使用putExtra

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

+0

當我使用單詞「溝通」時,我發言(寫道)。我不是說雙向交談。我會在一個新的問題上重述一下。感謝回覆。 – Andy 2011-02-10 19:09:19