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 android.support.v4.app.ServiceCompat; 32import android.support.v4.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 public void onClick(View v) { 99 startService(new Intent(LocalServiceBroadcaster.this, LocalService.class)); 100 } 101 }; 102 103 private OnClickListener mStopListener = new OnClickListener() { 104 public void onClick(View v) { 105 stopService(new Intent(LocalServiceBroadcaster.this, LocalService.class)); 106 } 107 }; 108 109 public static class LocalService extends Service { 110 LocalBroadcastManager mLocalBroadcastManager; 111 int mCurUpdate; 112 113 static final int MSG_UPDATE = 1; 114 115 Handler mHandler = new Handler() { 116 @Override 117 public void handleMessage(Message msg) { 118 switch (msg.what) { 119 case MSG_UPDATE: { 120 mCurUpdate++; 121 Intent intent = new Intent(ACTION_UPDATE); 122 intent.putExtra("value", mCurUpdate); 123 mLocalBroadcastManager.sendBroadcast(intent); 124 Message nmsg = mHandler.obtainMessage(MSG_UPDATE); 125 mHandler.sendMessageDelayed(nmsg, 1000); 126 } break; 127 default: 128 super.handleMessage(msg); 129 } 130 } 131 }; 132 133 @Override 134 public void onCreate() { 135 super.onCreate(); 136 mLocalBroadcastManager = LocalBroadcastManager.getInstance(this); 137 } 138 139 public int onStartCommand(Intent intent, int flags, int startId) { 140 // Tell any local interested parties about the start. 141 mLocalBroadcastManager.sendBroadcast(new Intent(ACTION_STARTED)); 142 143 // Prepare to do update reports. 144 mHandler.removeMessages(MSG_UPDATE); 145 Message msg = mHandler.obtainMessage(MSG_UPDATE); 146 mHandler.sendMessageDelayed(msg, 1000); 147 return ServiceCompat.START_STICKY; 148 } 149 150 @Override 151 public void onDestroy() { 152 super.onDestroy(); 153 154 // Tell any local interested parties about the stop. 155 mLocalBroadcastManager.sendBroadcast(new Intent(ACTION_STOPPED)); 156 157 // Stop doing updates. 158 mHandler.removeMessages(MSG_UPDATE); 159 } 160 161 @Override 162 public IBinder onBind(Intent intent) { 163 return null; 164 } 165 } 166} 167