如何定期唤醒我的应用程序 [英] How to wake up my App Periodically

查看:111
本文介绍了如何定期唤醒我的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Android中创建诸如提醒之类的功能.

I want to make an functionality, like reminder, in Android.

我想在我的应用程序/活动未运行或用户界面不可见时启动它.

I want to start-up my app/activity, when it is not running, or its UI is invisible.

与提醒类似,它会在需要的时间唤醒应用程序.

It is some-thing like same as reminder, that wakes ups the app at desired time.

我没有使用过任何类型的后台任务或服务, 所以我不知道该怎么办, 还是我应该学习哪种类型的课程或演示?

I have not worked with any type of background task or service, so I haven't any idea that what to do, or what type of classes or demos should be studied by me?

任何人都可以通过演示或教程链接给我一些建议. 谢谢,谢谢.

Can any one give me some suggestions with demos or tutorials links. Thanks, in advance.

推荐答案

您好,请使用以下代码.这是服务.通过将挂起的Intent与警报管理器一起使用,您可以在需要的时间打开UI.

Hi use the following code. This is service. By using pending Intent with alarm manager you can open your UI at your needed time.

import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
public class ScheduleCheckService extends Service{

    private Timer timer;
    final  int REFRESH=0;
    Context context;
    private PendingIntent pendingIntent;

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        context=this;
        //==============================================

        TimerTask refresher;
        // Initialization code in onCreate or similar:
        timer = new Timer();    
        refresher = new TimerTask() {
            public void run() {
              handler.sendEmptyMessage(0);
            };
        };
        // first event immediately,  following after 1 seconds each
        timer.scheduleAtFixedRate(refresher, 0,1000); 
        //=======================================================

    }

    final Handler handler = new Handler() {


        public void handleMessage(Message msg) {
              switch (msg.what) {
              case REFRESH: 
                   //your code here 


                  break;
              default:
                  break;
              }
          }
        };


         void PendingIntentmethod()
         {
         Intent myIntent = new Intent(context, YOURCLASS.class);        
         pendingIntent = PendingIntent.getActivity(context, 0, myIntent, 0);
         AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
        Calendar calendar = Calendar.getInstance();


         }




}

启动服务并在需要时停止服务,也不要忘记将其注册在清单文件中.

Start the service and stop the service when you want and also dont forget to register it in manifest file.

这篇关于如何定期唤醒我的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆