欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android實現系統(tǒng)消息推送

 更新時間:2020年07月22日 17:08:04   作者:VictorFactory  
這篇文章主要為大家詳細介紹了Android實現系統(tǒng)消息推送,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

現在好多應用都接入了推送功能,市面上也有很多關于推送的第三方,例如極光等等,那么我們需求不大,接入極光會造成很大的資源浪費,下面我們來看下利用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() {
    // 獲取系統(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; // 使用默認設置,比如鈴聲、震動、閃燈 
     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啟動頁廣告(騰訊廣告聯盟)解決方法及源碼下載

    Android啟動頁廣告(騰訊廣告聯盟)解決方法及源碼下載

    這篇文章主要介紹了Android啟動頁廣告(騰訊廣告聯盟)解決方法的相關資料,啟動頁幾乎成為了每個app的標配,有些商家在啟動頁中增加了開屏廣告以此帶來更多的收入,需要的朋友可以參考下
    2017-03-03
  • Android Studio安裝配置方法圖文詳細教程

    Android Studio安裝配置方法圖文詳細教程

    這篇文章主要為大家介紹了Android Studio下載和配置圖文詳細教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • Android內嵌Unity并實現互相跳轉的實例代碼

    Android內嵌Unity并實現互相跳轉的實例代碼

    這篇文章主要介紹了Android內嵌Unity并實現互相跳轉的實例代碼,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-11-11
  • Android獲取LinearLayout的寬度和高度示例代碼

    Android獲取LinearLayout的寬度和高度示例代碼

    這篇文章主要介紹了android獲取LinearLayout的寬度和高度,如果想直接獲取在布局文件中定義的組件的寬度和高度,可以直接使用View.getLayoutParams().width和View.getLayoutParams().height,本文結合示例代碼介紹的非常詳細,需要的朋友可以參考下
    2023-08-08
  • Android應用自動更新功能實現的方法

    Android應用自動更新功能實現的方法

    這篇文章主要為大家詳細介紹了Android應用自動更新功能實現的方法,感興趣的小伙伴們可以參考一下
    2016-06-06
  • 詳解Android Activity中的幾種監(jiān)聽器和實現方式

    詳解Android Activity中的幾種監(jiān)聽器和實現方式

    這篇文章主要介紹了Activity中的幾種監(jiān)聽器和實現方式的相關資料,幫助大家更好的理解和學習使用Android,感興趣的朋友可以了解下
    2021-04-04
  • Android中DrawerLayout+ViewPager滑動沖突的解決方法

    Android中DrawerLayout+ViewPager滑動沖突的解決方法

    這篇文章主要為大家詳細介紹了Android中DrawerLayout+ViewPager滑動沖突的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • Android實現層疊卡片式banner

    Android實現層疊卡片式banner

    這篇文章主要為大家詳細介紹了Android實現層疊卡片式banner,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-10-10
  • Android中的HTextView庫實現TextView動畫效果

    Android中的HTextView庫實現TextView動畫效果

    HTextView是一個用來給TextView里的文字做各種轉換動畫的開源庫,不僅提供了多種動畫選擇,而且還有重復字符的位移動畫,雖然并沒有多么復雜,但是它使用的這些典型的設計模式以及各種動畫的實現確實可以從中讓我們學到不少知識
    2023-12-12
  • Android 7.0 監(jiān)聽網絡變化的示例代碼

    Android 7.0 監(jiān)聽網絡變化的示例代碼

    這篇文章主要介紹了Android 7.0 監(jiān)聽網絡變化的示例代碼,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-03-03

最新評論