Android实现系统消息推送

 更新时间:2020年07月22日 17:08:04   作者:VictorFactory  
这篇文章主要为大家详细介绍了Android实现系统消息推送,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

现在好多应用都接入了推送功能,市面上也有很多关于推送的第三方,例如极光等等,那么我们需求不大,接入极光会造成很大的资源浪费,下面我们来看下利用android服务进行本地推送消息。

1.注册一个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() {
    // 获取系统的通知管理器 
    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; // 使用默认设置,比如铃声、震动、闪灯 
     notification.flags = Notification.FLAG_AUTO_CANCEL; // 但用户点击消息后,消息自动在通知栏自动消失 
    notification.flags |= Notification.FLAG_NO_CLEAR;// 点击通知栏的删除,消息不会依然不会被删除 
    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中注册

<service android:name=".ui.Service.PushSmsService"></service>

3.由于我是需要全局应用就在Application中进行启动了 

public void startService() {
  Intent intent = new Intent(this, PushSmsService.class);
  // 启动服务
  startService(intent);
 }

4.也可以配合服务端使用,定时推送消息

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:

相关文章

  • Android 自定义view实现水波纹动画效果

    Android 自定义view实现水波纹动画效果

    这篇文章主要介绍了 Android 自定义view实现水波纹动画效果的实例代码,非常不错,具有参考借鉴价值,需要的朋友参考下吧
    2017-01-01
  • Android控制界面刷新技巧汇总

    Android控制界面刷新技巧汇总

    这篇文章主要为大家详细介绍了Android控制界面刷新的小技巧,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-03-03
  • Android裁剪图像实现方法示例

    Android裁剪图像实现方法示例

    这篇文章主要介绍了Android裁剪图像实现方法,结合完整实例形式分析了Android针对图片的读取、调用、裁剪、保存等操作实现技巧,需要的朋友可以参考下
    2016-08-08
  • Android 获取实时网速实现详解

    Android 获取实时网速实现详解

    这篇文章主要为大家介绍了Android 获取实时网速实现详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-11-11
  • android获取附近蓝牙设备并计算距离的实例代码

    android获取附近蓝牙设备并计算距离的实例代码

    下面小编就为大家分享一篇android获取附近蓝牙设备并计算距离的实例代码,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-01-01
  • Android基础控件(EditView、SeekBar等)的使用方法

    Android基础控件(EditView、SeekBar等)的使用方法

    这篇文章主要介绍了Android基础控件的属性及使用方法,介绍了基础控件有TextView、ImageView、Button、EditView等,感兴趣的小伙伴们可以参考一下
    2016-03-03
  • ViewPager实现图片切换效果

    ViewPager实现图片切换效果

    这篇文章主要为大家详细介绍了ViewPager实现图片切换效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-09-09
  • Android调用前后摄像头同时工作实例代码

    Android调用前后摄像头同时工作实例代码

    本篇文章主要介绍了Android调用前后摄像头同时工作实例代码,这里整理了详细的代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
    2017-07-07
  • Android通过startService实现文件批量下载

    Android通过startService实现文件批量下载

    这篇文章主要为大家详细介绍了Android通过startService实现文件批量下载的示例,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2015-12-12
  • Android 设置应用全屏的两种解决方法

    Android 设置应用全屏的两种解决方法

    本篇文章小编为大家介绍,Android 设置应用全屏的两种解决方法。需要的朋友参考下
    2013-04-04

最新评论