5.Android消息推送机制简单例子

1.首先布局文件xml代码:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:paddingBottom="@dimen/activity_vertical_margin"
 7     android:paddingLeft="@dimen/activity_horizontal_margin"
 8     android:paddingRight="@dimen/activity_horizontal_margin"
 9     android:paddingTop="@dimen/activity_vertical_margin"
10     tools:context="com.example.riger.notificationdemo.MainActivity">
11 
12     <LinearLayout
13         android:orientation="vertical"
14         android:layout_width="match_parent"
15         android:layout_height="match_parent"
16         android:layout_alignParentTop="true"
17         android:layout_alignParentLeft="true"
18         android:layout_alignParentStart="true">
19 
20         <TextView
21             android:layout_width="wrap_content"
22             android:layout_height="wrap_content"
23             android:text="hello"
24             android:id="@+id/textView" />
25 
26         <Button
27             android:layout_width="match_parent"
28             android:layout_height="wrap_content"
29             android:text="通知"
30             android:id="@+id/btnStart" />
31 
32         <Button
33             android:layout_width="match_parent"
34             android:layout_height="wrap_content"
35             android:text="清除"
36             android:id="@+id/btnStop" />
37     </LinearLayout>
38 </RelativeLayout>

2.主界面代码MainActivity:

 1 public class MainActivity extends Activity implements View.OnClickListener{
 2 
 3     private Button btnStart;
 4     private Button btnStop;
 5     @Override
 6     protected void onCreate(Bundle savedInstanceState) {
 7         super.onCreate(savedInstanceState);
 8         setContentView(R.layout.activity_main);
 9         initView();
10     }
11 
12     public void initView(){
13         btnStart = (Button)findViewById(R.id.btnStart);
14         btnStop = (Button)findViewById(R.id.btnStop);
15         btnStart.setOnClickListener(this);
16         btnStop.setOnClickListener(this);
17     }
18 
19 
20     @Override
21     public void onClick(View view) {
22         int id = view.getId();
23         switch(id){
24             case R.id.btnStart:
25                 //启动服务
26                 Intent intent = new Intent();
27                 intent.setAction("ymw.MY_SERVICE");
28                 intent.setPackage("com.example.riger.notificationdemo");
29                 startService(intent);
30                 break;
31             case R.id.btnStop:
32                 Intent i = new Intent();
33                 i.setAction("ymw.MY_SERVICE");
34                 i.setPackage("com.example.riger.notificationdemo");
35                 stopService(i);
36                 break;
37             default:
38                 break;
39         }
40 
41     }
42 }

3.消息推送服务文件NotificationService

 1 public class NotificationService extends Service{
 2 
 3     private static final String TAG = NotificationService.class.getSimpleName();
 4     //获取消息线程
 5     private MessageThread messageThread = null;
 6 
 7     //点击查看
 8     private Intent messageIntent = null;
 9     private PendingIntent messagePendingIntent = null;
10 
11     //通知栏消息
12     private NotificationManager messageNotificationManage = null;
13 
14     @Nullable
15     @Override
16     public IBinder onBind(Intent intent) {
17         return null;
18     }
19 
20     @Override
21     public int onStartCommand(Intent intent, int flags, int startId) {
22         // 初始化
23         messageNotificationManage = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
24 
25         messageIntent = new Intent(this,MainActivity.class);
26         messagePendingIntent = PendingIntent.getActivity(this,0,messageIntent,0);
27 
28         // 开启线程
29         messageThread = new MessageThread();
30         messageThread.isRunning = true;
31         messageThread.start();
32 
33         return super.onStartCommand(intent,flags,startId);
34     }
35 
36     /**
37      * 从服务器获取信息
38      */
39     class MessageThread extends Thread{
40         public boolean isRunning = false;
41         public void run() {
42             try{
43                 Log.v(TAG,"111111111");
44                 // 间隔时间
45                 Thread.sleep(1000);
46                 // 获取服务器消息
47                 String serverMessage = getServerMessage();
48                 if (serverMessage != null && !"".equals(serverMessage)) {
49                     Log.v(TAG,"22222222");
50                     // 更新通知栏
51                     Notification.Builder builder = new Notification.Builder(NotificationService.this);
52                     builder.setSmallIcon(R.mipmap.ic_launcher);
53                     builder.setTicker("显示第一个通知");
54                     builder.setContentTitle("第一个通知");
55                     builder.setContentText("Hello World!");
56                     builder.setWhen(System.currentTimeMillis()); //发送时间
57                     builder.setDefaults(Notification.DEFAULT_ALL);
58                     Notification notification = builder.build();
59                     messageNotificationManage.notify(123, notification);
60                     Log.v(TAG,"33333333");
61                 }
62 
63             }catch (InterruptedException e) {
64                 e.printStackTrace();
65             }
66         }
67     }
68 
69     @Override
70     public void onDestroy(){
71         messageThread.isRunning = false;
72         super.onDestroy();
73     }
74 
75     /**
76      * 模拟发送信息
77      */
78     public String getServerMessage() {
79         return "HELLO WORLD!";
80     }
81 
82 }

最后记得配置下Service:

1 <service android:name=".NotificationService">
2             <intent-filter>
3                 <action android:name="ymw.MY_SERVICE"/>
4             </intent-filter>
5         </service>

运行效果:

      

原文地址:https://www.cnblogs.com/benchao/p/6111924.html