android广播的特点和Toast,Android 广播的两种注册方式

制作 MainActivity 的布局文件 acitvity_main,其完整代码如下:

android:orientation="vertical" android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:gravity="center_horizontal"

android:layout_gravity="center_horizontal"

android:text="Received Broadcasts" />

android:id="@+id/btnSendBroadcast"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:gravity="center_horizontal"

android:layout_gravity="center_horizontal"

android:text="Send Broadcast"/>

android:id="@+id/etReceivedBroadcast"

android:layout_width="match_parent"

android:layout_height="wrap_content" />

制作 MainActivity ,其完整代码如下:

package com.ccsoft.broadcastdemo;

import android.app.Activity;

import android.content.Intent;

import android.content.IntentFilter;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

public class MainActivity extends Activity implements View.OnClickListener {

MyReceiver myReceiver;

IntentFilter intentFilter;

EditText etReceivedBroadcast;

Button btnSendBroadcast;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activit_main);

etReceivedBroadcast = (EditText) findViewById(R.id.etReceivedBroadcast);

btnSendBroadcast = (Button) findViewById(R.id.btnSendBroadcast);

//keep reference to Activity context

MyApplication myApplication = (MyApplication) this.getApplicationContext();

myApplication.mainActivity = this;

btnSendBroadcast.setOnClickListener(this);

myReceiver = new MyReceiver();

intentFilter = new IntentFilter("chanchawReceiver");

}

@Override

protected void onResume() {

super.onResume();

registerReceiver(myReceiver, intentFilter);

}

@Override

protected void onPause() {

super.onPause();

unregisterReceiver(myReceiver);

}

@Override

public void onClick(View view) {

Intent intent = new Intent("chanchawReceiver");

sendBroadcast(intent);

}

}

上面步骤贴到项目中后会有些类、对象不存在造成报错,先别着急,布置完全部代码后即可正常运行。步骤2中用到的 MyApplication 的完整代码如下

package com.ccsoft.broadcastdemo;

import android.app.Application;

public class MyApplication extends Application {

@Override

public void onCreate() {

super.onCreate();

}

MainActivity mainActivity;

}

接收器 MyReceiver 的完整代码如下:

package com.ccsoft.broadcastdemo;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.util.Log;

import android.widget.Toast;

public class MyReceiver extends BroadcastReceiver {

private static final String TAG = "chanchaw";

@Override

public void onReceive(Context context, Intent intent) {

MainActivity mainActivity = ((MyApplication) context.getApplicationContext()).mainActivity;

mainActivity.etReceivedBroadcast.append("broadcast: "+intent.getAction()+"\n");

String msg = intent.getStringExtra("msg");

Toast.makeText(context, "接收到了!", Toast.LENGTH_LONG).show();

Log.i(TAG,"接收到广播的消息了!");

}

}

项目的清单文件 AndroidManifest.xml 的完整代码如下:

package="com.ccsoft.broadcastdemo">

android:name=".MyApplication"

android:allowBackup="true"

android:icon="@mipmap/ic_launcher"

android:label="@string/app_name"

android:roundIcon="@mipmap/ic_launcher_round"

android:supportsRtl="true"

android:theme="@style/AppTheme" >

最终形成的项目结构如下图:

1460000020600935

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值