Android BroadCastReciver学习笔记

来源:互联网 发布:百度bae数据库 编辑:程序博客网 时间:2024/06/06 14:26


MyReciver.java

package com.example.lesson4_broadcastreceiver;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;public class MyReciver extends BroadcastReceiver{@Overridepublic void onReceive(Context context, Intent intent) {// TODO Auto-generated method stubSystem.out.println(intent.getStringExtra("name").toString());}}

MainActivity.java

package com.example.lesson4_broadcastreceiver;import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity implements OnClickListener{private Button btnButton;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);btnButton = (Button)findViewById(R.id.button1);btnButton.setOnClickListener(this);}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.button1:  Intent intent = new Intent(MainActivity.this, MyReciver.class);intent.putExtra("name", "first");sendBroadcast(intent);break;default:break;}}}

AndroidManifest.xml

    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.example.lesson4_broadcastreceiver.MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>                <receiver android:name="MyReciver"></receiver>            </application>


原创粉丝点击