码迷,mamicode.com
首页 > 其他好文 > 详细

使用BroadercasterReceive(一)

时间:2015-11-24 21:15:07      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:

1.创建一个broadercaster

技术分享

package com.example.yabushan.broadercastreceiver;

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

public class MyReceiver extends BroadcastReceiver {
    public MyReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
       System.out.println("接收到的消息:"+intent.getStringExtra("DATA").toString());
    }
}

  

package com.example.yabushan.broadercastreceiver;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.deleverData).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.deleverData:
                Intent  intent=new Intent(this,MyReceiver.class);
                intent.putExtra("DATA","您好");
                sendBroadcast(intent);
                break;
        }
    }
}

  

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.yabushan.broadercastreceiver" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity android:name=".MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
  //需要在这里注册
        <receiver
            android:name=".MyReceiver"
            android:enabled="true"
            android:exported="true" >
        </receiver>
    </application>

</manifest>

 

使用BroadercasterReceive(一)

标签:

原文地址:http://www.cnblogs.com/yabushan/p/4992876.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!