AS中api23的消息推送机制和以前版本的不太一样,具体代码如下:
(一).activity_mail.xml布局文件,在这里面我用了二个按钮,一个是用来发送消息,还一个是用来断开服务,具体代码如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:fitsSystemWindows="true"
tools:context=".MainActivity">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="send"
android:onClick="sendNews"
android:layout_marginTop="36dp"
android:id="@+id/button"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="clean"
android:onClick="cleanNews"
android:layout_below="@+id/button"
android:layout_alignParentStart="true"
android:layout_marginTop="33dp" />
</RelativeLayout>
(二).然后就是MainActivity.java的内容了,这里面就实现了下点击事件,所以也没多少代码。
package com.example.administrator.mynotification;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendNews(View view){
Intent intent = new Intent();
intent.setAction("cjj_SERVICE");
startService(intent);
}
public void cleanNews(View view){
Intent intent = new Intent();
intent.setAction("cjj_SERVICE");
stopService(intent);
}
}
(三).接下就是创建服务了NotificationService.java,其中的有些代码和方法和以往的不同,具体如下:
package com.example.administrator.mynotification;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
/**
* Created by Administrator on 2015/10/30.
*/
public class NotificationService extends Service {
private Notification.Builder notification;
private Intent mintent;
private PendingIntent pendingIntent;
private NotificationManager notificationManager = null;
private MessageThread messageThread;
private int notificationID = 1;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
notification = new Notification.Builder(getApplicationContext());
notification.setSmallIcon(R.mipmap.ic_launcher);
notification.setContentTitle("来消息啦!");
notification.setDefaults(Notification.DEFAULT_SOUND);
mintent = new Intent(this,MainActivity.class);
pendingIntent =PendingIntent.getActivity(this,0,mintent,0);
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
messageThread = new MessageThread();
messageThread.start();
return super.onStartCommand(intent, flags, startId);
}
class MessageThread extends Thread{
public void run(){
notification.setContentText("已收到信息!");
notification.setContentIntent(pendingIntent);
notificationManager.notify(notificationID,notification.build());
notificationID++;
}
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
(四).最后就是在androidmanifest.xml文件中注册service:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.administrator.mynotification" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".NotificationService">
<intent-filter>
<action android:name="cjj_SERVICE"></action>
</intent-filter>
</service>
</application>
</manifest>
本人纯属菜鸟一个,刚入此行,如有写的不对,请大家多多指正。。