Android 实现Service自动启动
2016-10-10 11:00
225 查看
http://blog.csdn.net/yangwen123/article/details/8020912
版权声明:本文为博主原创文章,未经博主允许不得转载。
有些应用需要实现在安装完毕后自动后台启动,并且用户无法关闭,实现监听功能,那么这个又如何实现呢?上文介绍了开机启动应用程序,是采用开机完成发送的广播来实现的,本文在上文的基础上采用定时器广播实现服务无法关闭功能。
[html] view
plain copy
<!-- 开机启动完成广播接收器 -->
<receiver android:name="com.spreadtrum.BroadcastReceiver.BootReceiver" android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
<!-- 定时闹钟 广播接收器 -->
<receiver android:name="com.spreadtrum.BroadcastReceiver.AlarmReceiver">
<intent-filter>
<action android:name="com.spreadtrum.action.ALARM"></action>
</intent-filter>
</receiver>
[java] view
plain copy
public class AlarmReceiver extends BroadcastReceiver {
private static final String TAG = "AlarmReceiver";
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (intent.getAction().equals("com.spreadtrum.action.ALARM")) {
context.startService(new Intent("com.service.NetService"));
}
else if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, AlarmReceiver.class);
i.setAction("com.spreadtrum.action.ALARM");
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(),10*1000, PendingIntent.getBroadcast(context, 0,i,0));
}
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
有些应用需要实现在安装完毕后自动后台启动,并且用户无法关闭,实现监听功能,那么这个又如何实现呢?上文介绍了开机启动应用程序,是采用开机完成发送的广播来实现的,本文在上文的基础上采用定时器广播实现服务无法关闭功能。
[html] view
plain copy
<!-- 开机启动完成广播接收器 -->
<receiver android:name="com.spreadtrum.BroadcastReceiver.BootReceiver" android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
<!-- 定时闹钟 广播接收器 -->
<receiver android:name="com.spreadtrum.BroadcastReceiver.AlarmReceiver">
<intent-filter>
<action android:name="com.spreadtrum.action.ALARM"></action>
</intent-filter>
</receiver>
[java] view
plain copy
public class AlarmReceiver extends BroadcastReceiver {
private static final String TAG = "AlarmReceiver";
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (intent.getAction().equals("com.spreadtrum.action.ALARM")) {
context.startService(new Intent("com.service.NetService"));
}
else if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, AlarmReceiver.class);
i.setAction("com.spreadtrum.action.ALARM");
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(),10*1000, PendingIntent.getBroadcast(context, 0,i,0));
}
}
}
相关文章推荐
- android如何实现开机自动启动Service或app
- android如何实现开机自动启动Service或app
- android如何实现开机自动启动Service或app
- android如何实现开机自动启动Service或app
- Android中实现开机自动启动服务(service)实例
- 【转】android如何实现开机自动启动Service或app
- android如何实现开机自动启动Service或app
- android如何实现开机自动启动Service或app
- android如何实现开机自动启动Service或app
- android如何实现开机自动启动Service或app
- android如何实现开机自动启动Service或app
- android如何实现开机自动启动Service或app
- android如何实现开机自动启动Service或app
- android如何实现开机自动启动Service或app
- android如何实现开机自动启动Service或app(转)
- 【转】android如何实现开机自动启动Service或app
- android如何实现开机自动启动Service或app
- android如何实现开机自动启动Service或app
- Android 实现Service自动启动
- android如何实现开机自动启动Service或app