每5分钟后启动Android服务

KUL*_*ING 16 service android alarmmanager timertask

我在网上搜索了最近2天,但我找不到任何有用的教程.我已经创建了一个服务,我在服务启动时在状态栏中发送通知.我希望该服务在显示通知后停止,并在5分钟后再次启动.如果有可能请告诉我,如果有的话,请给我一些有用的教程.我听说过,TimerTask并且AlarmManager我也尝试使用它们,但我无法获得理想的结果.

编辑:我需要每5分钟启动一次服务,即使我的应用程序没有运行.

mah*_*mah 25

您不想使用a,TimerTask因为这取决于您的应用程序连续运行.的AlarmManager实现使得它的安全为您的应用程序执行之间被杀害.

声明你试图使用AlarmManager但没有得到理想的结果并不是一个有用的声明,因为它告诉没有人如何帮助你做到正确.表达发生的事情会更有用.

http://code4reference.com/2012/07/tutorial-on-android-alarmmanager/包含一些看似有用的教程AlarmManager.以下是重点:

1)您的警报会Intent在到期时触发.由您来决定Intent应该实现什么样的以及如何实现它.我提供的链接有一个基于BroadcastReceiver的完整示例.

2)您可以使用以下示例安装警报:

public void setOnetimeTimer(Context context) {
    AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
    intent.putExtra(ONE_TIME, Boolean.TRUE);
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
    am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (1000 * 60 * 5), pi);
}
Run Code Online (Sandbox Code Playgroud)

  • 很好的方法,真的.你应该这样使用AlarmManager;`Calendar wakeUpTime = Calendar.getInstance(); wakeUpTime.add(Calendar.SECOND,seconds); AlarmManager aMgr =(AlarmManager)getSystemService(ALARM_SERVICE); aMgr.set(AlarmManager.RTC_WAKEUP,wakeUpTime.getTimeInMillis(),pendingIntent);` (2认同)

Khy*_*nia 11

下面我提供了三个文件,MainActivity.java用于启动服务,第二个文件MyService.java为5 Minute和Third提供服务是清单文件.

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        startService(new Intent(this, MyService.class)); //start service which is MyService.java
    }
}
Run Code Online (Sandbox Code Playgroud)

MyService.java

 public class MyService extends Service {

    public static final int notify = 300000;  //interval between two services(Here Service run every 5 Minute)
    private Handler mHandler = new Handler();   //run on another Thread to avoid crash
    private Timer mTimer = null;    //timer handling

    @Override
    public IBinder onBind(Intent intent) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate() {
        if (mTimer != null) // Cancel if already existed
            mTimer.cancel();
        else
            mTimer = new Timer();   //recreate new
        mTimer.scheduleAtFixedRate(new TimeDisplay(), 0, notify);   //Schedule task
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mTimer.cancel();    //For Cancel Timer
        Toast.makeText(this, "Service is Destroyed", Toast.LENGTH_SHORT).show();
    }

    //class TimeDisplay for handling task
    class TimeDisplay extends TimerTask {
        @Override
        public void run() {
            // run on another thread
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    // display toast
                    Toast.makeText(MyService.this, "Service is running", Toast.LENGTH_SHORT).show();
                }
            });
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

AndroidManifest.xml中

  <service android:name=".MyService" android:enabled="true" android:exported="true"></service>
Run Code Online (Sandbox Code Playgroud)

  • 服务何时完成,我想在五分钟后没有用户输入的情况下再次执行服务? (2认同)