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

Broadcast使用以及在通知栏显示消息

时间:2014-12-26 16:17:23      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:

本实例只有1个界面(Activity),界面上包涵一个EditText及一个按钮。当按钮被按下时,获取EditText中的内容并广播。本实例还创建了一个BroadcastReceiver,用于接收按钮按下时的广播,并将广播消息内容显示于通知栏。当点击通知栏的广播消息时另一个Activity将会被打开。完成后显示效果如下:

技术分享  技术分享

编程实现

1、新建一个android项目(过程省略),添加需要的文件。添加后,此项目工程目录如下:

技术分享

2、编辑activity_main.xml,添加一个EditText和一个按钮,完成后内容大致如下:

<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="com.broadcastreceiver.broadcastreceiverproject.MainActivity$PlaceholderFragment" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:ems="10"
        android:hint="@string/textEdit1_text"
        android:inputType="text" >

    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="14dp"
        android:text="@string/button1_text" />

</RelativeLayout>

3、编辑MainActivity.java文件,MainActivity.java中定义了类MainActivity。MainActivity继承自Activity,以activity_main.xml定义的界面为显示界面。当activity_main.xml上的按钮按下时,获取EditText的内容并发送广播。MainActivity.java内容如下:

package com.broadcastreceiver.broadcastreceiverproject;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.content.Context;
import android.content.Intent;

public class MainActivity extends Activity {
    
    private Context mContext;
    private Button btnSendBroadcast;
    private EditText etBroadcastContent;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        mContext = this;
        btnSendBroadcast = (Button)findViewById(R.id.button1);
        btnSendBroadcast.setOnClickListener(new btnSendBroadcast_Listener());
        
        etBroadcastContent = (EditText)findViewById(R.id.editText1);
    }
    
    private class btnSendBroadcast_Listener implements View.OnClickListener
    {
        @Override
        public void onClick(View v)
        {
            String content = etBroadcastContent.getText().toString().trim();
            if (content.length() < 1)
            {
                Toast.makeText(mContext, etBroadcastContent.getHint(), Toast.LENGTH_SHORT).show();
                return;
            }
            
            Intent intent = new Intent();
            intent.setAction("com.eoeandroid.action.BroadcastReceiverTest");
            intent.putExtra("msg_content", content);
            sendBroadcast(intent);
        }
    }
}

4、编辑HelloBroadcastReceiver.java文件,此文件中定义了广播接收器BroadcastReceiver,用于接收MainActivity中发送的广播。接收到广播后,将广播的内容显示与通知栏。HelloBroadcastReceiver.java文件内容如下:

package com.broadcastreceiver.broadcastreceiverproject;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.app.NotificationManager;
import android.app.Notification;
import android.app.PendingIntent;

public class HelloBroadcastReceiver extends BroadcastReceiver
{
    private Context context;
    
    @Override
    public void onReceive(Context context, Intent intent)
    {
        this.context = context;
        showNotification(intent);
    }
    
    //通知栏显示消息
    private void showNotification(Intent intent)
    {
        NotificationManager notificationManager = (NotificationManager)context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        
        //一组将被执行的Intent集合
        Intent aIntent[] = new Intent[1];
        aIntent[0] = new Intent(context, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivities(context, 0, aIntent, 0);
        
        String title = intent.getExtras().getString("msg_content");
        Notification noti = new Notification.Builder(context)
                .setContentTitle(title.subSequence(0, title.length()))
                .setContentText(title.subSequence(0, title.length()))
                .setSmallIcon(R.drawable.ic_launcher)
                .setWhen(System.currentTimeMillis())
                .setContentIntent(pendingIntent) //调用后当通知栏消息被点击时,Intent集合被执行
                .build();
        
        notificationManager.notify(R.layout.activity_main, noti);
    }
}

5、最后需要注册广播接收器,本实例采用静态方式注册。在AndroidManifest.xml的<application>内添加以下内容:

        <receiver android:name=".HelloBroadcastReceiver" android:exported="false">
            <intent-filter>
                <action android:name="com.eoeandroid.action.BroadcastReceiverTest"/>
            </intent-filter>
        </receiver>

 

Broadcast使用以及在通知栏显示消息

标签:

原文地址:http://www.cnblogs.com/xiaojihua20/p/4186882.html

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