Java tutorial
/* * Copyright (C) 2016 Teclib' * * This file is part of Flyve MDM Android. * * Flyve MDM Android is a subproject of Flyve MDM. Flyve MDM is a mobile * device management software. * * Flyve MDM Android is free software: you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 3 * of the License, or (at your option) any later version. * * Flyve MDM Android is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * ------------------------------------------------------------------------------ * @author Dorian LARGET * @copyright Copyright (c) 2016 Flyve MDM * @license GPLv3 https://www.gnu.org/licenses/gpl-3.0.html * @link https://github.com/flyvemdm/flyvemdm-android * @link http://www.glpi-project.org/ * ------------------------------------------------------------------------------ */ package com.teclib.service; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.IBinder; import android.support.v4.app.NotificationCompat; import android.widget.RemoteViews; import com.teclib.api.FlyveLog; import com.teclib.database.SharedPreferenceAction; import com.teclib.flyvemdm.MQTTNotifierActivity; import com.teclib.flyvemdm.R; public class NotificationAdminRequest extends Service { final static String ACTION = "NotificationAdminRequest"; private SharedPreferenceAction sharedPreferenceAction; final static String STOP_SERVICE_BROADCAST_KEY = "StopServiceAdminBroadcastKey"; final static int RQS_STOP_SERVICE = 1; NotifyServiceReceiver notifyServiceReceiver; @Override public void onCreate() { notifyServiceReceiver = new NotifyServiceReceiver(); super.onCreate(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { sharedPreferenceAction = new SharedPreferenceAction(); IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(ACTION); registerReceiver(notifyServiceReceiver, intentFilter); FlyveLog.d(sharedPreferenceAction.getApks(getBaseContext()).toString()); if (sharedPreferenceAction.getApks(getBaseContext()).toString().equals("[null]")) { FlyveLog.e("notification whitout sharedpreference apk"); } else { CustomNotification(); } return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { this.unregisterReceiver(notifyServiceReceiver); super.onDestroy(); } @Override public IBinder onBind(Intent arg0) { return null; } public void CustomNotification() { RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notification_install_apps); Intent intent = new Intent(this, MQTTNotifierActivity.class); PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder builder = new NotificationCompat.Builder(this) .setSmallIcon(R.mipmap.ic_white_stork).setTicker(getString(R.string.installAsk_string)) .setOngoing(true).setContentIntent(pIntent).setAutoCancel(true).setContent(remoteViews); Notification notificationInstall = builder.build(); notificationInstall.flags |= Notification.FLAG_AUTO_CANCEL; remoteViews.setImageViewResource(R.id.imagenotileft, R.mipmap.ic_notification_install_apps); remoteViews.setTextViewText(R.id.title, getString(R.string.app_name)); remoteViews.setTextViewText(R.id.text, getString(R.string.installAsk_string)); NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); notificationmanager.notify(6, builder.build()); } public class NotifyServiceReceiver extends BroadcastReceiver { @Override public void onReceive(Context arg0, Intent arg1) { stopSelf(); } } }