繁体   English   中英

无法从活动发送广播:android

[英]unable to send Broadcast from activity :android

朋友们,我无法将广播从一个活动发送到其他活动请参阅下面的代码并提供帮助:

      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("!!!!!!!InchooTutorial@@@@@@@$$$$","%%%%%%% 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>

由于当您发送广播时其他活动未运行,您将不会收到它。 如果您希望即使活动未运行也要接收广播。 在xml中声明它。

这是给你的代码。 我希望这就是你想要的。

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);

    }

}

显示Toast的第二个活动

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>

如果创建了SendBroadcast活动,则将发送广播。

然后启动第二个名为ToastDisplay的活动,并在onResume中注册BroadcastReceiver。 但这是为了迟到,广播已经发送,它不会停留在系统中!

尝试发送如下的粘贴广播:

sendStickyBroadcast(Intent)

或者在清单中声明broadcastreceiver然后你需要创建一个扩展BroadcastReceiver类的单独的类,这不能被继承。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM