2012-04-24 117 views
0

我有問題,發送廣播從一個活動到其他..its不工作我的代碼收到是below..pls參考吧..無法從活動發送廣播到其他:Android的

發送類是:

public class SendBroadcast extends Activity { 
public static String BROADCAST_ACTION = "com.unitedcoders.android.broadcasttest.SHOWTOAST"; 

/** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    Button button1 = (Button) findViewById(R.id.button1); 
    button1.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View view) { 
     sendBroadcast(); 
     } 

    }); 
} 




    public void sendBroadcast(){ 

    Intent broadcast = new Intent(); 
    broadcast.setAction("com.unitedcoders.android.broadcasttest.SHOWTOAST"); 
    sendBroadcast(broadcast); 
} 

}

和reciving類是:

public class ToastDisplay extends Activity { 

private BroadcastReceiver receiver = new BroadcastReceiver() { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     Log.i("asdasd","sdasdasd"); 
     // TODO Auto-generated method stub 
     Toast.makeText(getApplicationContext(), "%%%%%%%%%%%%received", Toast.LENGTH_SHORT).show(); 

    } 
}; 

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

    super.onResume(); 
} 

@Override 
protected void onPause() { 
    unregisterReceiver(receiver); 
    super.onPause(); 
} 

}

清單文件::::

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.unitedcoders.android.broadcasttest" 
    android:versionCode="1" 
    android:versionName="1.0"> 
<uses-sdk android:minSdkVersion="4" /> 

    <application android:icon="@drawable/icon" android:label="@string/app_name"> 
    <activity android:name=".SendBroadcast" 
       android:label="@string/app_name"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity android:name=".ToastDisplay"> 

    <intent-filter> 
     <action android:name="com.unitedcoders.android.broadcasttest.SHOWTOAST"> </action> 
    </intent-filter> 
    </activity> 

    </application> 
    </manifest> 
+0

u能解釋什麼ü希望在按鈕做 – 2012-04-24 10:06:01

+0

點擊我發送一個廣播,並在簡歷註冊廣播接收器......但OnRecive()方法是在接受工作邊...我卡在這裏所以請幫助.. – SRam 2012-04-24 10:09:06

+0

是否有可能使用廣播reciver? – SRam 2012-04-24 10:13:12

回答

1
您尚未註冊您的接收器在清單

:如果你想播放從一個活動到另一個數據註冊爲

<receiver android:name="receiver"> 
<intent-filter> 
<action 
android:name="com.unitedcoders.android.broadcasttest.SHOWTOAST"/> 
</intent-filter> 
</receiver> 
+0

你好imran我已經註冊我的收件人在我的代碼在ToastDislay類的onResume方法()請看看, ... :( – SRam 2012-04-24 10:57:23

+0

這是你的課ToastDislay – 2012-04-24 10:58:48

-1

,只是讓使用意圖。在你想要發送數據的第二個活動的onDestroy方法中,創建Intent對象並以額外的意圖廣播數據,然後在你的broadcastReceiver類中使用intent.getExtra()方法。

有關詳細信息: follow this tutorial

相關問題