Android实现电蚊香/Service服务管理类(一)
实现这个实例需要的就是Service这个类的管理,我们用到的是启动Service,并在退出应用程序的时候关闭(Stop)Service,下面我们首先看下这个程序的运行截图:
图中显示的控件一个是ImageView,另一个是ImageButton,我们点击ImageButton之后可以控制程序的运行和关闭,这里我们看到的是关闭的状态。
当我们点击ImageButton之后,程序开始运行,并且在通知栏有相应的显示。
下面给出实现的截图:
下面给出实现的代码:
1.Service类
package irdc.ex10_08;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.os.IBinder;
/* 自定义MyService继承Service */
public class MyService extends Service
{
private String MY_PREFS = "MosPre";
private NotificationManager notiManager;
private int mosStatus;