Example usage for android.content Context stopService

List of usage examples for android.content Context stopService

Introduction

In this page you can find the example usage for android.content Context stopService.

Prototype

public abstract boolean stopService(Intent service);

Source Link

Document

Request that a given application service be stopped.

Usage

From source file:com.drinviewer.droiddrinviewer.DrinViewerBroadcastReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
        NetworkInfo networkInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
        if (networkInfo.isConnected()) {
            /**/* ww  w  .  ja  v  a 2s  .co m*/
             * WiFi is connected, get its broadcast 
             * address and start the discovery process
             * repeated at a fixed time interval
             */
            wifiBroadcastAddress = getWiFiBroadcastAddress(context);
            startAlarmRepeater(context);
        } else {
            wifiBroadcastAddress = null;
        }
    } else if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
        NetworkInfo networkInfo = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
        if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI && !networkInfo.isConnected()) {
            /**
             * WiFi is disconnected, stop the discovery
             * process repeating, it would be a waste of resources
             */
            wifiBroadcastAddress = null;
            stopAlarmRepeater(context);
        }
    } else if (intent.getAction().equals(context.getResources().getString(R.string.broadcast_startdiscovery))
            || intent.getAction()
                    .equals(context.getResources().getString(R.string.broadcast_cleanhostcollection))) {

        boolean startService = true;
        /**
         * Calls the DiscoverServerService asking to do a discovery
         * or a clean host collection by simply forwarding the received action
         */
        Intent service = new Intent(context, DiscoverServerService.class);
        service.setAction(intent.getAction());

        if (intent.getAction().equals(context.getResources().getString(R.string.broadcast_startdiscovery))) {

            ConnectivityManager connManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            wifiBroadcastAddress = (mWifi.isConnected()) ? getWiFiBroadcastAddress(context) : null;
            startService = wifiBroadcastAddress != null;
            service.putExtra("wifiBroadcastAddress", wifiBroadcastAddress);
        }

        if (startService)
            startWakefulService(context, service);

        if (intent.getBooleanExtra("stopservice", false)) {
            context.stopService(service);
        }
    } else if (intent.getAction()
            .equals(context.getResources().getString(R.string.broadcast_startalarmrepeater))) {
        /**
         * start the alarm repeater only if WiFi is connected already
         * used by ServerListFragment.onServiceConnected method to start the discovery
         * if the application is launched being already connected to a WiFi network
         */
        ConnectivityManager connManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

        if (mWifi.isConnected()) {
            // if we're called from the activity, try to get a broadcast address
            if (intent.getBooleanExtra("forcegetbroadcast", false))
                wifiBroadcastAddress = getWiFiBroadcastAddress(context);
            startAlarmRepeater(context);
        } else {
            wifiBroadcastAddress = null;
        }
    } else if (intent.getAction()
            .equals(context.getResources().getString(R.string.broadcast_stopalarmrepeater))) {
        /**
         *  stop the alarm repeater. period.
         *  used by DrinViewerApplication.onTerminate method
         */
        wifiBroadcastAddress = null;
        stopAlarmRepeater(context);
    }
}

From source file:nl.hnogames.domoticz.Utils.NotificationUtil.java

public static void sendSimpleNotification(String title, String text, final Context context) {
    if (UsefulBits.isEmpty(title) || UsefulBits.isEmpty(text) || context == null)
        return;//w  ww .  j  av a 2  s.co  m
    if (prefUtil == null)
        prefUtil = new SharedPrefUtil(context);
    prefUtil.addUniqueReceivedNotification(text);
    prefUtil.addLoggedNotification(new SimpleDateFormat("yyyy-MM-dd hh:mm ").format(new Date()) + text);

    List<String> suppressedNot = prefUtil.getSuppressedNotifications();
    List<String> alarmNot = prefUtil.getAlarmNotifications();
    try {
        if (prefUtil.isNotificationsEnabled() && suppressedNot != null && !suppressedNot.contains(text)) {
            NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                    .setSmallIcon(R.drawable.domoticz_white)
                    .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher))
                    .setContentTitle(alarmNot != null && alarmNot.contains(text)
                            ? context.getString(R.string.alarm) + ": " + title
                            : title)
                    .setContentText(alarmNot != null && alarmNot.contains(text)
                            ? context.getString(R.string.alarm) + ": " + text
                            : text)
                    .setGroupSummary(true).setGroup(GROUP_KEY_NOTIFICATIONS).setAutoCancel(true);

            if (!prefUtil.OverWriteNotifications())
                NOTIFICATION_ID = text.hashCode();
            if (prefUtil.getNotificationVibrate())
                builder.setDefaults(NotificationCompat.DEFAULT_VIBRATE);
            if (!UsefulBits.isEmpty(prefUtil.getNotificationSound()))
                builder.setSound(Uri.parse(prefUtil.getNotificationSound()));

            Intent targetIntent = new Intent(context, MainActivity.class);
            PendingIntent contentIntent = PendingIntent.getActivity(context, 0, targetIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            builder.setContentIntent(contentIntent);

            if (prefUtil.isNotificationsEnabled() && alarmNot != null && alarmNot.contains(text)) {
                Intent stopAlarmIntent = new Intent(context, StopAlarmButtonListener.class);
                PendingIntent pendingAlarmIntent = PendingIntent.getBroadcast(context, 78578, stopAlarmIntent,
                        PendingIntent.FLAG_UPDATE_CURRENT);
                builder.addAction(android.R.drawable.ic_delete, "Stop", pendingAlarmIntent);
            }

            if (prefUtil.showAutoNotifications()) {
                builder.extend(new NotificationCompat.CarExtender()
                        .setUnreadConversation(getUnreadConversation(context, text)));
            }

            NotificationManagerCompat.from(context).notify(NOTIFICATION_ID, builder.build());
            if (prefUtil.isNotificationsEnabled() && alarmNot != null && alarmNot.contains(text)) {
                Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
                if (alert == null) {
                    alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
                    if (alert == null)
                        alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
                }
                if (alert != null) {
                    Intent ringtoneServiceStartIntent = new Intent(context, RingtonePlayingService.class);
                    ringtoneServiceStartIntent.putExtra("ringtone-uri", alert.toString());
                    context.startService(ringtoneServiceStartIntent);

                    if (prefUtil.getAlarmTimer() > 0) {
                        Thread.sleep(prefUtil.getAlarmTimer() * 1000);
                        Intent stopIntent = new Intent(context, RingtonePlayingService.class);
                        context.stopService(stopIntent);
                    }
                }
            }
        }
    } catch (Exception ex) {
        Log.i("NOTIFY", ex.getMessage());
    }
}

From source file:de.vanita5.twittnuker.util.Utils.java

public static void startRefreshServiceIfNeeded(final Context context) {
    final Intent refreshServiceIntent = new Intent(context, RefreshService.class);
    if (isNetworkAvailable(context) && hasAutoRefreshAccounts(context) && !isPushEnabled(context)) {
        if (isDebugBuild()) {
            Log.d(LOGTAG, "Start background refresh service");
        }//from  w w  w. j  ava2  s .co  m
        context.startService(refreshServiceIntent);
    } else {
        context.stopService(refreshServiceIntent);
    }
}

From source file:org.getlantern.firetweet.util.Utils.java

public static void startRefreshServiceIfNeeded(final Context context) {
    final Intent refreshServiceIntent = new Intent(context, RefreshService.class);
    if (isNetworkAvailable(context) && hasAutoRefreshAccounts(context)) {
        if (isDebugBuild()) {
            Log.d(LOGTAG, "Start background refresh service");
        }/*  w  ww. j  av a2 s. c  o  m*/
        context.startService(refreshServiceIntent);
    } else {
        context.stopService(refreshServiceIntent);
    }
}