从服务设置警报:
package yourPackage;import android.app.Service;import android.content.Context;import android.content.Intent;import android.os.IBinder;public class YourService extends Service{ Alarm alarm = new Alarm(); public void onCreate() { super.onCreate();} @Override public int onStartCommand(Intent intent, int flags, int startId) { alarm.setAlarm(this); return START_STICKY; } @Overridepublic void onStart(Intent intent, int startId) { alarm.setAlarm(this); } @Override public IBinder onBind(Intent intent) { return null; }}
如果要设置在电话启动时重复发出警报:
向
Manifest.xml添加权限和服务:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>...<receiver android:name=".AutoStart"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"></action> </intent-filter></receiver>...<service android:name=".YourService" android:enabled="true" android:process=":your_service" ></service>
并创建一个新类:
package yourPackage;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;public class AutoStart extends BroadcastReceiver{ Alarm alarm = new Alarm(); @Override public void onReceive(Context context, Intent intent) {if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) { alarm.setAlarm(context); } }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)