AlarmManager android调用特定方法

时间:2016-07-18 07:30:45

标签: java android android-studio alarmmanager android-alarms

我想设置一个在30秒后开始执行特定方法的Alarmmanager ..

例如:

//在此闹钟中,我想调用SpecificMehtod();

Intent intent1 = new Intent(context, AutoStartUp.class);
        PendingIntent pintent = PendingIntent.getService(context, 0, intent1, 0);
        AlarmManager alarm = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal_alarm.getTimeInMillis(), 24 * 60 * 60 * 1000, pintent);//ripeto l'alarm ogni 24 ore



public void SpecificMethod(){
   //Some code here
}

抱歉我的英文=)

2 个答案:

答案 0 :(得分:0)

您可以执行此操作,为其添加额外内容,并将用户发送到@RestController @Api(tags = "index") @RequestMapping("/index") public class IndexController { @Autowired private IndexService indexService; @RequestMapping(value = "/data", method = RequestMethod.GET) @ApiOperation(value="today's data",notes="today's data",consumes="application/json",produces="application/json") public Object getTodayData() { return indexService.getTodayData(); } @RequestMapping(value = "/chartData", method = RequestMethod.GET) @ApiOperation(value="charts data",notes="charts data",consumes="application/json",produces="application/json") public Object getLast7Data() { return indexService.getLast7Data(); } } 活动:

public class Scanner extends CordovaPlugin {
   ....
    private BroadcastReceiver mReceiver = null;

    @Override
    public boolean execute(String action, JSONArray data, CallbackContext callbackContext) throws JSONException {
       ....
       mReceiver = new BarcodeReceiver(callbackContext);
       ....
    }
   ....
}

public class BarcodeReceiver extends BroadcastReceiver {

    private CallbackContext callbackContext;

    public BarcodeReceiver (CallbackContext callbackContext) {
        this.callbackContext = callbackContext;
    }

    public void onReceive(Context ctx, Intent intent) {
        if (intent.getAction().equals(ACTION_BARCODE_SERVICE_BROADCAST)) {
            strBarcode = intent.getExtras().getString(KEY_BARCODE_STR);
            callbackContext.success(strBarcode);
        }
    }
} 

AutoStartUp活动中,您可以通过检查额外内容来启动该方法:

intent.putExtra("name","specificMethod");
startActivity(intent);

答案 1 :(得分:0)

您可以创建广播接收器并使用警报管理器

进行设置
Intent intent = new Intent(this, AutoStartUp.class);   
 PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,  intent, PendingIntent.FLAG_UPDATE_CURRENT);
 alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),10*60*1000, pendingIntent); 

她是你的广播接收班

public class AutoStartUp extends BroadcastReceiver{


@Override
public void onReceive(Context context, Intent intent) {
       specificMethod();

}

}

不要忘记将其注册为主要内容

<receiver android:name=".AutoStartUp"
                      android:enabled="true"
                    > 
            </receiver>