📄 alarmservice_service.java
字号:
/* * Copyright (C) 2007 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.google.android.samples.app;// Need the following import to get access to the app resources, since this// class is in a sub-package.import com.google.android.samples.R;import android.app.Notification;import android.app.NotificationManager;import android.app.Service;import android.content.Intent;import android.os.BinderNative;import android.os.IBinder;import android.os.Parcel;/** * This is an example of implementing an application service that will run in * response to an alarm, allowing us to move long duration work out of an * intent receiver. * * @see AlarmService * @see AlarmService_Alarm */public class AlarmService_Service extends Service implements Runnable{ @Override protected void onCreate() { mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); // This is who should be launched if the user selects our persistent // notification. Intent intent = new Intent(this, AlarmService.class); // We are going to display a notification for the entire time that we // are running. mNM.notifyWithText(R.string.alarm_service_started, getText(R.string.alarm_service_started), NotificationManager.LENGTH_SHORT, new Notification( R.drawable.stat_sample, getText(R.string.alarm_service_label), intent, null, null)); // Start up the thread running the service. Note that we create a // separate thread because the service normally runs in the process's // main thread, which we don't want to block. Thread thr = new Thread(null, this, "AlarmService_Service"); thr.start(); } @Override protected void onDestroy() { // Cancel the persistent notification. mNM.cancel(R.string.alarm_service_started); // Tell the user we stopped. mNM.notifyWithText(R.string.alarm_service_finished, getText(R.string.alarm_service_finished), NotificationManager.LENGTH_SHORT, null); } public void run() { // Normally we would do some work here... for our sample, we will // just sleep for 30 seconds. long endTime = System.currentTimeMillis() + 15*1000; while (System.currentTimeMillis() < endTime) { synchronized (mBinder) { try { mBinder.wait(endTime - System.currentTimeMillis()); } catch (Exception e) { } } } // Done with our work... stop the service! this.stopSelf(); } @Override public IBinder getBinder() { return mBinder; } // This is the object that receives interactions from clients. See // RemoteService for a more complete example. private final IBinder mBinder = new BinderNative() { @Override protected boolean onTransact(int code, Parcel data, Parcel reply, int flags) { return super.onTransact(code, data, reply, flags); } }; private NotificationManager mNM;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -