标签:
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>
标签:
原文地址:http://www.cnblogs.com/yabushan/p/4992876.html