Android實(shí)現(xiàn)系統(tǒng)消息推送
現(xiàn)在好多應(yīng)用都接入了推送功能,市面上也有很多關(guān)于推送的第三方,例如極光等等,那么我們需求不大,接入極光會(huì)造成很大的資源浪費(fèi),下面我們來看下利用android服務(wù)進(jìn)行本地推送消息。
1.注冊(cè)一個(gè)Service
import android.annotation.TargetApi;
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.Build;
import android.os.IBinder;
import java.util.Calendar;
/**
* Created by 70883 on 2017/8/10.
*/
public class PushSmsService extends Service {
private NotificationManager manager;
private PendingIntent pi;
private MyThread myThread;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
myThread = new MyThread();
myThread.start();
super.onCreate();
}
@Override
public void onDestroy() {
super.onDestroy();
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void notification() {
// 獲取系統(tǒng)的通知管理器
manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(getApplicationContext(),
MainActivity.class);
pi = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
Notification notification = new Notification.Builder(getApplicationContext())
.setAutoCancel(true)
.setContentText("工作在忙,也要吃飯哦")
.setContentIntent(pi)
.setSmallIcon(R.mipmap.ic_icon)
.setWhen(System.currentTimeMillis())
.build();
notification.defaults = Notification.DEFAULT_ALL; // 使用默認(rèn)設(shè)置,比如鈴聲、震動(dòng)、閃燈
notification.flags = Notification.FLAG_AUTO_CANCEL; // 但用戶點(diǎn)擊消息后,消息自動(dòng)在通知欄自動(dòng)消失
notification.flags |= Notification.FLAG_NO_CLEAR;// 點(diǎn)擊通知欄的刪除,消息不會(huì)依然不會(huì)被刪除
manager.notify(0, notification);
}
private class MyThread extends Thread{
private Calendar c ;
@Override
public void run() {
while (true){
c = Calendar.getInstance();
if(c.get(Calendar.HOUR_OF_DAY) == 15){
try {
notification();
sleep(1000*60*60);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
2.在AndroidMan中注冊(cè)
<service android:name=".ui.Service.PushSmsService"></service>
3.由于我是需要全局應(yīng)用就在Application中進(jìn)行啟動(dòng)了
public void startService() {
Intent intent = new Intent(this, PushSmsService.class);
// 啟動(dòng)服務(wù)
startService(intent);
}
4.也可以配合服務(wù)端使用,定時(shí)推送消息
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android 跨進(jìn)程SharedPreferences異常詳解
這篇文章主要介紹了Android 跨進(jìn)程SharedPreferences異常詳解的相關(guān)資料,需要的朋友可以參考下2017-05-05
Android Walker登錄記住密碼頁(yè)面功能實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了Android Walker登錄記住密碼頁(yè)面功能的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05
Android自定義view實(shí)現(xiàn)日歷打卡簽到
這篇文章主要為大家詳細(xì)介紹了Android自定義view實(shí)現(xiàn)日歷打卡簽到,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05
Kotlin類的繼承實(shí)現(xiàn)詳細(xì)介紹
這篇文章主要介紹了Kotlin類的繼承,在Java中類的繼承默認(rèn)是繼承父類的方法和參數(shù)的,但是在kotlin中默認(rèn)是不繼承的,那么我們接下來來驗(yàn)證2022-09-09
Android直播系統(tǒng)平臺(tái)搭建之圖片實(shí)現(xiàn)陰影效果的方法小結(jié)
這篇文章主要介紹了Android直播系統(tǒng)平臺(tái)搭建, 圖片實(shí)現(xiàn)陰影效果的若干種方法,本文給大家?guī)砣N方法,每種方法通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-08-08
Android開發(fā)實(shí)現(xiàn)布局幀布局霓虹燈效果示例
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)布局幀布局霓虹燈效果,涉及Android界面布局、資源文件操作及屬性設(shè)置等相關(guān)技巧,需要的朋友可以參考下2019-04-04
Android獲取系統(tǒng)儲(chǔ)存以及內(nèi)存信息的方法(二)
這篇文章主要為大家詳細(xì)介紹了Android獲取系統(tǒng)儲(chǔ)存以及內(nèi)存信息的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10

