最近在写一个小程序,需求是手机摇一摇就弹窗出来。第一次使用了Service,学习了两天,实现了Service弹窗,开机启动,Activity启动和销毁。满足了自己的需求。现记录学习心得。希望能给你带来一些帮助。
1.Service创建:重写4个方法
package com.service;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class PopupService extends Service {
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
super.onCreate();
System.out.println("Service is Created");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("Service is Started");
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
System.out.println("Service is Destroyed");
}
}2.Service配置:在AndroidManifest.xml中声明
<service
android:name="com.service.PopupService"
android:priority = "1000" <!-- 提高优先级-->
android:persistent="true"> <!-- 免杀,不知道有没有起作用-->
<intent-filter>
<action android:name="com.service.POPUP_SERVICE" />
</intent-filter>
</service>文件创建:
package com.service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class StartupReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
// 启动一个Service
Intent serviceIntent = new Intent(context, PopupService.class);
context.startService(serviceIntent);
}
}
<receiver android:name="com.service.StartupReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</receiver> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
4.Service从Activity中启动:
在主Activity中使用startService(Intent)
Intent popupintent=new Intent();
popupintent.setAction("com.service.POPUP_SERVICE");
startService(popupintent);弹窗相关代码:
Intent activityIntent = new Intent(this, SelectFriendsActivity.class); //要想在Service中启动Activity,必须设置如下标志 activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(activityIntent);
完整代码:
package com.service;
import com.task.SelectFriendsActivity;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.IBinder;
import android.os.Vibrator;
import android.util.Log;
public class PopupService extends Service implements SensorEventListener{
<span style="font-family:Microsoft YaHei;"> </span>//sensorManager
<span style="font-family:Microsoft YaHei;"> </span>private SensorManager sensorManager;
<span style="font-family:Microsoft YaHei;"> </span>private Vibrator vibrator;
<span style="font-family:Microsoft YaHei;"> </span>Intent activityIntent;
<span style="font-family:Microsoft YaHei;"> </span>@Override
<span style="font-family:Microsoft YaHei;"> </span>public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
<span style="font-family:Microsoft YaHei;"> </span>}
@Override
public void onCreate() {
super.onCreate();
System.out.println("Service is Created");
//<span style="font-family:Microsoft YaHei;"></span>监听器
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("Service is Started");
//启动service,将serviceon置为TRUE,可弹窗。
SelectFriendsActivity.serviceon = true;
if (sensorManager != null) {// 注册监听器
sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
// 第一个参数是Listener,第二个参数是所得传感器类型,第三个参数值获取传感器信息的频率
}
<pre name="code" class="java"> activityIntent = new Intent(this, SelectFriendsActivity.class);
// 要想在Service中启动Activity,必须设置如下标志
activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 可以使用这个style:
<style name="dialogTheme" parent="android:Theme.Dialog">
<item name="android:windowNoTitle">true</item>
</style>
弹窗要求添加权限:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
6.Service 保持一直运行,不被杀死 的方法:
重写onDestroy():
@Override
public void onDestroy() {
// super.onDestroy();
// System.out.println("Service is Destroyed");
<font size="4" face="Microsoft YaHei"><span style="font-family:Microsoft YaHei;font-size:18px;"><span style="font-family:Microsoft YaHei;font-size:18px;"><span style="font-family:Microsoft YaHei;font-size:18px;"></span></span></span></font><pre name="code" class="java"> System.out.println("Service is Destroyed,and is Restarted"); Intent localIntent = new Intent(); localIntent.setClass(this, PopupService.class); //销毁时重新启动Service this.startService(localIntent); }
这样无论如何Service都一直在后台运行了。
7.图文记录:
7.1 启动app,MainActivity中调用了startService(popupintent);
结果:说明依次调用了Service中的onCreate()、onStartCommand()方法,Service开始运行
7.2 震动手机,Service响应弹窗
结果:弹窗,Service正常运行,重力感应器正常运行
7.3 杀死应用进程,震动手机,Service仍然响应弹窗
结果:弹窗,说明虽然应用进程被杀死,但是Service仍保持正常运行,重力感应器正常运行
7.4 点击弹窗的Avtivity中的按钮,按钮监听代码:
Intent popupintent=new Intent();
popupintent.setAction("com.service.POPUP_SERVICE");
stopService(popupintent);即调用了stopService(Intent) 方法
结果:打印出Service
is Destroyed,说明调用了Service的onDestroy()方法
7.5 再次震动手机,发现还是有打印输出:
结果:说名虽然调用了onDestroy()方法,但是其Context未被清除,Service仍然存在
7.6 杀死所有有关此弹窗的进程,再次震动手机,发现没有打印输出了:
结果:一旦其Context被完全清除,Service就真正停止了。
感谢newcj 的博客 文章 Android 中的 Service 全面总结 让我受益匪浅。大家可以去看看,评论区很精彩:
【Android开发日记】初次探秘Android Service!Service开机启动+重力感应+弹窗+保持运行
原文:http://blog.csdn.net/geeklei/article/details/39749799