LocalServiceBroadcaster.java revision ac5fe7c617c66850fff75a9fce9979c6e5674b0f
1/* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.example.android.supportv4.content; 18 19import com.example.android.supportv4.R; 20 21import android.app.Activity; 22import android.app.Service; 23import android.content.BroadcastReceiver; 24import android.content.Context; 25import android.content.Intent; 26import android.content.IntentFilter; 27import android.os.Bundle; 28import android.os.Handler; 29import android.os.IBinder; 30import android.os.Message; 31import androidx.core.app.ServiceCompat; 32import androidx.localbroadcastmanager.content.LocalBroadcastManager; 33import android.view.View; 34import android.view.View.OnClickListener; 35import android.widget.Button; 36import android.widget.TextView; 37 38/** 39 * Demonstrates the use of a LocalBroadcastManager to easily communicate 40 * data from a service to any other interested code. 41 */ 42public class LocalServiceBroadcaster extends Activity { 43 static final String ACTION_STARTED = "com.example.android.supportv4.STARTED"; 44 static final String ACTION_UPDATE = "com.example.android.supportv4.UPDATE"; 45 static final String ACTION_STOPPED = "com.example.android.supportv4.STOPPED"; 46 47 LocalBroadcastManager mLocalBroadcastManager; 48 BroadcastReceiver mReceiver; 49 50 @Override 51 protected void onCreate(Bundle savedInstanceState) { 52 super.onCreate(savedInstanceState); 53 54 setContentView(R.layout.local_service_broadcaster); 55 56 // This is where we print the data we get back. 57 final TextView callbackData = (TextView)findViewById(R.id.callback); 58 59 // Put in some initial text. 60 callbackData.setText("No broadcast received yet"); 61 62 // We use this to send broadcasts within our local process. 63 mLocalBroadcastManager = LocalBroadcastManager.getInstance(this); 64 65 // We are going to watch for interesting local broadcasts. 66 IntentFilter filter = new IntentFilter(); 67 filter.addAction(ACTION_STARTED); 68 filter.addAction(ACTION_UPDATE); 69 filter.addAction(ACTION_STOPPED); 70 mReceiver = new BroadcastReceiver() { 71 @Override 72 public void onReceive(Context context, Intent intent) { 73 if (intent.getAction().equals(ACTION_STARTED)) { 74 callbackData.setText("STARTED"); 75 } else if (intent.getAction().equals(ACTION_UPDATE)) { 76 callbackData.setText("Got update: " + intent.getIntExtra("value", 0)); 77 } else if (intent.getAction().equals(ACTION_STOPPED)) { 78 callbackData.setText("STOPPED"); 79 } 80 } 81 }; 82 mLocalBroadcastManager.registerReceiver(mReceiver, filter); 83 84 // Watch for button clicks. 85 Button button = (Button)findViewById(R.id.start); 86 button.setOnClickListener(mStartListener); 87 button = (Button)findViewById(R.id.stop); 88 button.setOnClickListener(mStopListener); 89 } 90 91 @Override 92 protected void onDestroy() { 93 super.onDestroy(); 94 mLocalBroadcastManager.unregisterReceiver(mReceiver); 95 } 96 97 private OnClickListener mStartListener = new OnClickListener() { 98 @Override 99 public void onClick(View v) { 100 startService(new Intent(LocalServiceBroadcaster.this, LocalService.class)); 101 } 102 }; 103 104 private OnClickListener mStopListener = new OnClickListener() { 105 @Override 106 public void onClick(View v) { 107 stopService(new Intent(LocalServiceBroadcaster.this, LocalService.class)); 108 } 109 }; 110 111 public static class LocalService extends Service { 112 LocalBroadcastManager mLocalBroadcastManager; 113 int mCurUpdate; 114 115 static final int MSG_UPDATE = 1; 116 117 Handler mHandler = new Handler() { 118 @Override 119 public void handleMessage(Message msg) { 120 switch (msg.what) { 121 case MSG_UPDATE: { 122 mCurUpdate++; 123 Intent intent = new Intent(ACTION_UPDATE); 124 intent.putExtra("value", mCurUpdate); 125 mLocalBroadcastManager.sendBroadcast(intent); 126 Message nmsg = mHandler.obtainMessage(MSG_UPDATE); 127 mHandler.sendMessageDelayed(nmsg, 1000); 128 } break; 129 default: 130 super.handleMessage(msg); 131 } 132 } 133 }; 134 135 @Override 136 public void onCreate() { 137 super.onCreate(); 138 mLocalBroadcastManager = LocalBroadcastManager.getInstance(this); 139 } 140 141 @Override 142 public int onStartCommand(Intent intent, int flags, int startId) { 143 // Tell any local interested parties about the start. 144 mLocalBroadcastManager.sendBroadcast(new Intent(ACTION_STARTED)); 145 146 // Prepare to do update reports. 147 mHandler.removeMessages(MSG_UPDATE); 148 Message msg = mHandler.obtainMessage(MSG_UPDATE); 149 mHandler.sendMessageDelayed(msg, 1000); 150 return ServiceCompat.START_STICKY; 151 } 152 153 @Override 154 public void onDestroy() { 155 super.onDestroy(); 156 157 // Tell any local interested parties about the stop. 158 mLocalBroadcastManager.sendBroadcast(new Intent(ACTION_STOPPED)); 159 160 // Stop doing updates. 161 mHandler.removeMessages(MSG_UPDATE); 162 } 163 164 @Override 165 public IBinder onBind(Intent intent) { 166 return null; 167 } 168 } 169} 170