첫째, 결과의 효과 :
정보를 입력 방송하고, 다음을 클릭합니다 "브로드 캐스트 보내기", 그것은있을 수 안드로이드 방송 형태의 감소를 볼 수있는 페이지의 알림.
둘째, 구현 단계 :
1. 첫째, 문서 디렉토리 전에 다음 항목의 설립과 함께 : HelloBroadcast.java 및 MainActivity.java , MainActivity.java 제어 main.xml에를 , 그리고 HelloBroadcasrReceiver.java 주로 이전 데이터 수집 및 브로드 캐스트를 보낼 수 있습니다.
2. 에서 의 AndroidManifest.xml은 아래 <프로그램> 선언 된 브로드 캐스트 , 다음 방법 (이것은 정적 등록 및 등록 동적 인 자바 파일)
<receiver android:name=".HelloBroadcastReceiver">
<intent-filter>
<action android:name="com.HoD.action.BroadcastReceiverTest" />
</intent-filter>
</receiver>
3. main.xml에의 레이아웃 파일. 다음과 같이 컨트롤을 설정합니다 :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText android:id="@+id/et_broadcastContent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="请输入广播内容" />
<Button android:id="@+id/btn_sendBroadcast"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="发送广播" />
</LinearLayout>
4. 열기MainActivity.java의파일, 다음 코드를 작성 :
package com.HoD.broadcastreceiver;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
//因为步骤操作相对较多,故取得对象实例化信息与操作数据分开写,则需要存入变量
private Context mContext;
private Button btnSendBroadcast;
private TextView etBroadcastContent;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//
mContext = this;
//获取Button实例化对象,并设置监听事件
btnSendBroadcast = (Button) findViewById(R.id.btn_sendBroadcast);
btnSendBroadcast.setOnClickListener(new SendBroadcastClickListener());
//获取TextView的实例化对象
etBroadcastContent = (TextView) findViewById(R.id.et_broadcastContent);
}
//声明SendBroadcastClickListener类使用OnClickListener接口
private class SendBroadcastClickListener implements OnClickListener{
@Override
public void onClick(View v){
//获取文本框内内容
String content = etBroadcastContent.getText().toString().trim();
if(content.length() < 1){
//取出里面“Hint”字符串用Toast方法显示
Toast.makeText(mContext, etBroadcastContent.getHint(), 1).show();
return;
}
Intent intent = new Intent();
//运行HelloBroadcatReceiver
intent.setAction("com.HoD.action.BroadcastReceiverTest");
intent.putExtra("content", content);
sendBroadcast(intent);
}
}
}
5. HelloBroadcastReceiver은 삭제 작업을 상속 한 후 브로드 캐스트 리시버를 다음과 같이 클래스의 특정 코드 :
package com.HoD.broadcastreceiver;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class HelloBroadcastReceiver extends BroadcastReceiver{
private Context context;
@Override
//使用onReceive()方法接收数据
public void onReceive(Context context, Intent intent){
this.context = context;
showNotification(intent);
}
private void showNotification(Intent intent){
//通过getSystemService()方法获取NotificationManager服务
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
//创建一个Notification对象,并为其设置各种属性
//获取传递的图标、数据和当时的系统时间
//这种方式已经过时,不过先以体会思想为主
Notification notification = new Notification(R.drawable.ic_launcher, intent.getExtras().getString("content"), System.currentTimeMillis());
//该语句的作用是定义了一个不是当即显示的activity,只有当用户拉下notify显示列表,并且单击对应的项的时候,才会触发系统跳转到该activity
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0);
//notification.setLatestEventInfo(this, title, content, contentIntent);在此处设置在nority列表里的该norifycation得显示情况。
notification.setLatestEventInfo(context, intent.getExtras().getString("content"), null, pendingIntent);
//通过NotificationManager类的notify()方法将通知发送到状态栏
notificationManager.notify(R.layout.main, notification);
}
}
6. 발생하는 문제는, 사용하기 전에 패키지 요구 플러스 몇 가지 방법입니다 청소 잘.