Android学習ノートの放送意図と放送受信者MyBroadcastReceiver、Broadcast
6322 ワード
(1)第1種xmlファイルを用いた登録
レイアウトファイル、buttonクリックを追加するときにブロードキャスト
MainActivity.java
MyBroadcastReceiver.javaブロードキャストの受信者は、BroadcastReceiverクラスを継承し、onReceiveメソッドを再ロードします.
BOMファイルにブロードキャストを登録するには、次の手順に従います.
(2)2つ目はJavaコードで登録
レイアウトファイルは変更されません(buttonがクリックしたときにブロードキャストを行うことを含む)、リストファイルは操作されません.MyBroadcastReceiver.javaファイルは変更されません.受信に成功したかどうかをテストするのに適しています.:
MainActivity.java
レイアウトファイル、buttonクリックを追加するときにブロードキャスト
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="68dp"
android:layout_marginTop="82dp"
android:text="Button" />
</RelativeLayout>
MainActivity.java
package com.lc.broadcastreceiver_demo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.example.broadcastreceiver_demo.R;
/*
* xml ,
* <receiver android:name="com.lc.broadcastreceiver_demo.MyBroadcastReceiver" >
<intent-filter>
<action android:name="com.freedie.broadcast" />
</intent-filter>
</receiver>
*/
public class MainActivity extends Activity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) this.findViewById(R.id.button1);
/*
*
*/
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (R.id.button1 == v.getId()) {
Intent intent = new Intent();
intent.setAction("com.freedie.broadcast");
sendBroadcast(intent); //
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
MyBroadcastReceiver.javaブロードキャストの受信者は、BroadcastReceiverクラスを継承し、onReceiveメソッドを再ロードします.
package com.lc.broadcastreceiver_demo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
/*
* , ,onReceive
*/
public class MyBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "MyBroadcastReceiver";
@Override
public void onReceive(Context context, Intent intent) {
if ("com.freedie.broadcast".equals(intent.getAction())) {
Log.d(TAG, "com.freedie.broadcast Intent was receiverd!");
}
}
}
BOMファイルにブロードキャストを登録するには、次の手順に従います.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.broadcastreceiver_demo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.lc.broadcastreceiver_demo.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>
<!-- MyBroadcastReceiver -->
<receiver android:name="com.lc.broadcastreceiver_demo.MyBroadcastReceiver" >
<intent-filter>
<action android:name="com.freedie.broadcast" />
</intent-filter>
</receiver>
</application>
</manifest>
(2)2つ目はJavaコードで登録
レイアウトファイルは変更されません(buttonがクリックしたときにブロードキャストを行うことを含む)、リストファイルは操作されません.MyBroadcastReceiver.javaファイルは変更されません.受信に成功したかどうかをテストするのに適しています.:
MainActivity.java
package com.example.broadcastreceiver_javacode;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button button;
private MyBroadcastReceiver myBroadcastReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) this.findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (R.id.button1 == v.getId()) {
Intent intent = new Intent();
intent.setAction("com.freedie.brodcast");
sendBroadcast(intent); //
}
}
});
}
/*
*
*/
@Override
protected void onResume() {
super.onResume();
myBroadcastReceiver = new MyBroadcastReceiver();
//
IntentFilter filter = new IntentFilter();
filter.addAction("com.freedie.brodcast");
//
registerReceiver(myBroadcastReceiver, filter);
}
/*
*
*/
@Override
protected void onStop() {
super.onStop();
unregisterReceiver(myBroadcastReceiver);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}