码迷,mamicode.com
首页 > Windows程序 > 详细

API 23之消息推送机制

时间:2015-11-01 12:40:24      阅读:396      评论:0      收藏:0      [点我收藏+]

标签:

 

作为一个菜鸟,每个星期写一篇随笔来记录这周所学是很有必要的:

1.gson解析json,重点在于构建json数据的实体类,不过现在android studio里有gson Formart这个快速构建实体类的工具了,所以比较容易上手。

2.thinkandroid框架中的联网请求,同步(SyncHttpClient),异步(AsyncHttpClient),但可能有点过时了,毕竟这个框架几年都没有更新维护了。

3.自定义空间及属性。

4.glide图片加载缓存,okhttp联网请求及其封装的类OkHttpClientManager。

5.以上几点主要是jar包什么的,其实基本上就那么几个固定的方法。所以着重讲的是消息推送机制,由于使用的API是23的,所以跟以往的不太一样。具体如下:

(一).首先是activity_main.xml布局:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout
 3     xmlns:android="http://schemas.android.com/apk/res/android"
 4     xmlns:app="http://schemas.android.com/apk/res-auto"
 5     xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
 6     android:layout_height="match_parent" android:fitsSystemWindows="true"

 7     tools:context=".MainActivity">
 8 
 9     <Button
10         android:layout_width="fill_parent"
11         android:layout_height="wrap_content"
12         android:text="send"
13         android:onClick="sendNews"
14         android:layout_marginTop="36dp"
15         android:id="@+id/button"
16         android:layout_alignParentTop="true"
17         android:layout_alignParentStart="true" />
18     <Button
19         android:layout_width="fill_parent"
20         android:layout_height="wrap_content"
21         android:text="clean"
22         android:onClick="cleanNews"
23         android:layout_below="@+id/button"
24         android:layout_alignParentStart="true"
25         android:layout_marginTop="33dp" />
26 
27 </RelativeLayout>

 

(二).然后就是MainActivity.java文件:

 1 package com.example.administrator.mynotification;
 2 
 3 import android.app.Activity;
 4 import android.content.Intent;
 5 import android.os.Bundle;
 6 import android.support.design.widget.FloatingActionButton;
 7 import android.support.design.widget.Snackbar;
 8 import android.support.v7.app.AppCompatActivity;
 9 import android.support.v7.widget.Toolbar;
10 import android.view.View;
11 import android.view.Menu;
12 import android.view.MenuItem;
13 
14 public class MainActivity extends Activity {
15 
16     @Override
17     protected void onCreate(Bundle savedInstanceState) {
18         super.onCreate(savedInstanceState);
19         setContentView(R.layout.activity_main);
20     }
21     public void sendNews(View view){
22         Intent intent = new Intent();
23         intent.setAction("cjj_SERVICE");
24         startService(intent);
25     }
26     public void cleanNews(View view){
27         Intent intent = new Intent();
28         intent.setAction("cjj_SERVICE");
29         stopService(intent);
30     }
31 }

 

(三).最后就是NotificationService文件:

 1 package com.example.administrator.mynotification;
 2 
 3 import android.app.Notification;
 4 import android.app.NotificationManager;
 5 import android.app.PendingIntent;
 6 import android.app.Service;
 7 import android.content.Context;
 8 import android.content.Intent;
 9 import android.os.IBinder;
10 
11 /**
12  * Created by Administrator on 2015/10/30.
13  */
14 public class NotificationService extends Service {
15     private Notification.Builder notification;
16     private Intent mintent;
17     private PendingIntent pendingIntent;
18     private NotificationManager notificationManager = null;
19     private MessageThread messageThread;
20     private int notificationID = 1;
21 
22     @Override
23     public IBinder onBind(Intent intent) {
24         return null;
25     }
26 
27     @Override
28     public int onStartCommand(Intent intent, int flags, int startId) {
29         notification = new Notification.Builder(getApplicationContext());
30         notification.setSmallIcon(R.mipmap.ic_launcher);
31         notification.setContentTitle("来消息啦!");
32         notification.setDefaults(Notification.DEFAULT_SOUND);
33         mintent = new Intent(this,MainActivity.class);
34         pendingIntent =PendingIntent.getActivity(this,0,mintent,0);
35         notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
36         messageThread = new MessageThread();
37         messageThread.start();
38         return super.onStartCommand(intent, flags, startId);
39 
40 
41     }
42     class MessageThread extends Thread{
43         public void run(){
44             notification.setContentText("已收到信息!");
45             notification.setContentIntent(pendingIntent);
46             notificationManager.notify(notificationID,notification.build());
47             notificationID++;
48         }
49     }
50 
51     @Override
52     public void onDestroy() {
53         super.onDestroy();
54     }
55 }

 

(四).当然也不能忘记在manifest.xml文件中注册service

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3     package="com.example.administrator.mynotification" >
 4 
 5     <application
 6         android:allowBackup="true"
 7         android:icon="@mipmap/ic_launcher"
 8         android:label="@string/app_name"
 9         android:supportsRtl="true"
10         android:theme="@style/AppTheme" >
11         <activity
12             android:name=".MainActivity"
13             android:label="@string/app_name"
14             android:theme="@style/AppTheme.NoActionBar" >
15             <intent-filter>
16                 <action android:name="android.intent.action.MAIN" />
17 
18                 <category android:name="android.intent.category.LAUNCHER" />
19             </intent-filter>
20         </activity>
21         <service android:name=".NotificationService">
22             <intent-filter>
23                 <action android:name="cjj_SERVICE"></action>
24             </intent-filter>
25         </service>
26     </application>
27 
28 </manifest>

 

API 23之消息推送机制

标签:

原文地址:http://www.cnblogs.com/up513194962/p/4927446.html

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