首先建一个activity,布局.xml文件的格式如图:
代码如下:
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 tools:context="com.ghp.practice4_tuisong.MainActivity" > 6 <LinearLayout 7 android:layout_width="match_parent" 8 android:layout_height="wrap_content" 9 android:orientation="vertical" > 10 <Button 11 android:id="@+id/btsend" 12 android:layout_width="wrap_content" 13 android:layout_height="wrap_content" 14 android:text="发送消息" /> 15 <Button 16 android:id="@+id/btRegister" 17 android:layout_width="wrap_content" 18 android:layout_height="wrap_content" 19 android:text="订阅消息" /> 20 <Button 21 android:id="@+id/btUnReg" 22 android:layout_width="wrap_content" 23 android:layout_height="wrap_content" 24 android:text="取消订阅" /> 25 </LinearLayout> 26 </RelativeLayout>
在activity里,首先对这3个button注册事件:
1 private void initView(){ 2 myReceiver=new MyReceiver(); 3 btsend=(Button) findViewById(R.id.btsend); 4 btRegister=(Button) findViewById(R.id.btRegister); 5 btUnReg=(Button) findViewById(R.id.btUnReg); 6 btsend.setOnClickListener(this); 7 btRegister.setOnClickListener(this); 8 btUnReg.setOnClickListener(this); 9 }
然后分别对3个onclick处理:
1 @Override 2 public void onClick(View v) { 3 // TODO Auto-generated method stub 4 switch (v.getId()) { 5 case R.id.btsend: 6 //推送消息 7 sendBroadCast(); 8 break; 9 case R.id.btRegister: 10 //手动代码注册发送 11 reg(); 12 break; 13 case R.id.btUnReg: 14 unReg(); 15 break; 16 default: 17 break; 18 } 19 } 20 public void sendBroadCast() { 21 Intent intent=new Intent(); 22 intent.setAction(Intent.ACTION_EDIT);//类型 23 intent.putExtra("topic", "最新消息"); 24 intent.putExtra("msg", "出来了"); 25 super.sendBroadcast(intent); 26 } 27 public void reg(){ 28 29 IntentFilter intentFilter=new IntentFilter(); 30 intentFilter.addAction(Intent.ACTION_EDIT); 31 //intentFilter.addAction("wakeup"); 32 super.registerReceiver(myReceiver, intentFilter); 33 } 34 35 public void unReg(){ 36 super.unregisterReceiver(myReceiver); 37 }
上面这些是最简单的,下面是重点:
需要自己建立一个MyReceiver,继承BroadcastReceiver
代码如下,注释已详细讲解:
1 package com.ghp.receiver; 2 3 import java.util.Random; 4 5 import com.ghp.practice4_tuisong.MainActivity; 6 import com.ghp.practice4_tuisong.R; 7 8 import android.app.NotificationManager; 9 import android.app.PendingIntent; 10 import android.content.BroadcastReceiver; 11 import android.content.Context; 12 import android.content.Intent; 13 import android.media.RingtoneManager; 14 import android.net.Uri; 15 import android.support.v4.app.NotificationCompat; 16 import android.support.v4.app.TaskStackBuilder; 17 import android.widget.Toast; 18 19 public class MyReceiver extends BroadcastReceiver { 20 private String msg; 21 private String topic; 22 @Override 23 public void onReceive(Context context, Intent intent) { 24 // TODO Auto-generated method stub 25 //根据匹配类型处理 26 27 if(intent.getAction().equals(Intent.ACTION_EDIT)){ 28 topic=intent.getStringExtra("topic"); 29 msg=intent.getStringExtra("msg"); 30 Toast.makeText(context, topic+":"+msg, Toast.LENGTH_SHORT).show(); 31 32 } 33 34 //获取设置的提示音,用的是系统声音 35 Uri alarmSound=RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 36 /** 37 * 初始化通知提示框的布局,即在NotificationCompat.Builder对象中为通知指定UI信息和动作 38 * 布局里有提示图标,一般是应用的logo,提示的标题,及内容,有声音设置,震动设置 39 * 所有其他通知设置和内容都是可选的,具体可参考API NotificationCompat.Builder类 40 */ 41 NotificationCompat.Builder nBuilder=new NotificationCompat.Builder(context) 42 .setSmallIcon(R.drawable.ic_launcher).setContentText(msg).setContentTitle(topic) 43 .setSound(alarmSound).setVibrate(new long[]{500,500,500,500}).setAutoCancel(true); 44 45 //单击消息后连接打开的Avtivity 46 Intent reslutIntent=new Intent(context,MainActivity.class); 47 reslutIntent.putExtra("topic", topic); 48 reslutIntent.putExtra("msg", msg); 49 50 //添加动作到通知,当用户点击通知时,触发 51 //在通知里面,这个动作通过PendingIntent来定义 52 //想要启动Activity,可以通过调用setContentIntent()来添加一个PendingIntent 53 TaskStackBuilder stackBuilder=TaskStackBuilder.create(context);// 54 stackBuilder.addParentStack(MainActivity.class); 55 stackBuilder.addNextIntent(reslutIntent); 56 PendingIntent resultPendingIntent=stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 57 nBuilder.setContentIntent(resultPendingIntent);//设置显示框内容点击后对应的任务目标,及进入相应的activity 58 59 /** 60 * 消息管理器:NotificationManager 61 * nBuilder.build(): 发送前必须先使用NotificationCompat.Builder.build()来创建通知 62 * 这个方法会返回一个Notification对象 63 * 为了发布通知,可以通过调用NotificationManager.notify()来传递Notification对象到系统中 64 */ 65 NotificationManager mNotificationManager=(NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE); 66 Random r=new Random(); 67 mNotificationManager.notify(r.nextInt(), nBuilder.build());//发生通知,产生消息框 68 } 69 70 }
以上是一种方式,还有一种方式,是在AndroidManifest.xml设置:
首先添加权限:
<!-- 震动 --> <uses-permission android:name="android.permission.VIBRATE"/>
然后设置:
<!--配置文件:自动注册发送 --> <receiver android:name="com.ghp.receiver.MyReceiver"> <intent-filter> <action android:name="android.intent.action.EDIT"/> <action android:name="wakeup"/> </intent-filter> </receiver>
以上是简单的本地推送,一般是通过第三方推送消息,可以用极光推送和云推送等
时间: 2024-08-05 10:03:33