2012-04-25 53 views
3

喜的朋友,我不能從一個活動到其他活動發送廣播請參見下面我的代碼和幫助:無法從活動發送廣播:安卓

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

/*  } 
    }); 
} 



    public void sendBroadcast(){ 

    Intent broadcast = new Intent("com.unitedcoders.android.broadcasttest.SHOWTOAST"); 
    this.sendBroadcast(broadcast); 
    //startActivity(broadcast); 



} 

}

接收端代碼:

public class ToastDisplay extends Activity { 

private BroadcastReceiver mReceiver; 

@Override 
protected void onResume() { 

    // TODO Auto-generated method stub 
    super.onResume(); 
    Log.i("[email protected]@@@@@@$$$$","%%%%%%% msg_for_me"); 

ent // String msg_for_me = intent.getStringExtra(「some_msg」); //記錄我們的留言值 Log.i(「!!!!!!! InchooTutorial @@@@@@@ $$$$」,「%%%%%%% msg_for_me」);

 } 
    }; 
    //registering our receiver 
    this.registerReceiver(mReceiver, intentFilter); 
} 

    @Override 
    protected void onPause() { 
    // TODO Auto-generated method stub 
    super.onPause(); 
    //unregister our receiver 
    this.unregisterReceiver(this.mReceiver); 
} 

} 

的Manifest.xml是:

<?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" /> 
    <uses-permission android:name="android.permission.BROADCAST_STICKY"/> 
    <application android:icon="@drawable/icon" android:label="@string/app_name"> 
    <activity android:name=".SendBroadcast" 
       android:label="@string/app_name"> 
      <intent-filter> 
      nitedcoders.android.broadcasttest.SHOWTOAST" /> 
    </application>  </manifest> 
+0

您是否已將接收器添加到清單中? – 2012-04-25 09:40:12

+0

請參考我的清單文件.. – shyam 2012-04-25 09:57:49

回答

6

由於其它活動不運行時你寄廣播ü不會接受它。 即使活動未運行,您仍想收到廣播。用xml聲明它。

這裏是你的代碼。我希望這是你想要的。如果創建您的SendBroadcast活動的廣播將被髮送

package com.pdd.Receiver; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 

public class ReceiverActivity extends Activity implements OnClickListener{ 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     Button b=(Button) findViewById(R.id.button1); 
     b.setOnClickListener(this); 
    } 

    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     Intent i =new Intent("com.pdd.receiver.myaction"); 
     sendBroadcast(i); 
    } 
} 

接收機類

package com.pdd.Receiver; 

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.widget.Toast; 

public class MyReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context arg0, Intent arg1) { 
     // TODO Auto-generated method stub 

     //Intent i=new Intent(MyReceiver.class,Second.class); 
     Intent i=new Intent(arg0,Second.class); 
     i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     arg0.startActivity(i); 

    } 

} 

次活動顯示吐司

package com.pdd.Receiver; 

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

public class Second extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 

     Toast.makeText(getApplicationContext(), "This is second activity", 5000).show(); 
    } 
} 

清單文件

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.pdd.Receiver" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk android:minSdkVersion="8" /> 

    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" > 

     <receiver android:name="com.pdd.Receiver.MyReceiver"> 
      <intent-filter> 
       <action android:name="com.pdd.receiver.myaction"></action> 
      </intent-filter> 
     </receiver> 
     <activity 
      android:name=".ReceiverActivity" 
      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=".Second"></activity> 
    </application> 

</manifest> 
+0

你的意思是在manifest.xml中?請參考我的Manifestfile.xml,如果你可以對其進行修改.. – shyam 2012-04-25 10:05:55

+0

我應該在SendBroadcast()方法中啓動Activity嗎? – shyam 2012-04-25 10:07:29

+0

這裏是一個 Pratik 2012-04-25 15:04:29

1

然後,您開始稱爲ToastDisplay的第二個活動,並在onResume中註冊BroadcastReceiver。但是這是遲到了,廣播已經發送了,它不會留在系統中!

嘗試發送一個stickybroadcast,如:

sendStickyBroadcast(Intent) 

或者申報清單中的廣播接收器,但是,那麼你需要創建一個擴展的BroadcastReceiver類獨立的類,這不能inherreted。

+0

我欣賞我的努力和良好的理解,但sendStickyBroadcast(意圖)也不工作.. :( – shyam 2012-04-25 10:02:58

+0

public void sendBroadcast(){ Intent broadcast = new Intent(「com.unitedcoders.android .broadcasttest.SHOWTOAST「); this.sendStickyBroadcast(廣播); \t \t \t \t } – shyam 2012-04-25 10:08:30