开发者

how to automatically invoke my application in android

I want to au开发者_如何学Ctomatically invoke my application at 12.00 am,This kind of process ,how to implement in android,is it possible?


Look at AlarmManager.

Here is an example:

Intent myIntent = new Intent(getApplicationContext(), YourActivity.class);              
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 1,  myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, time, pendingIntent);


ReminderBootUp.java file have the code to schedule your activity at particular time

     package com.app.reminder;

        import java.text.SimpleDateFormat;
        import java.util.Date;

        import android.app.AlarmManager;
        import android.app.PendingIntent;
        import android.content.BroadcastReceiver;
        import android.content.Context;
        import android.content.Intent;
        import android.database.Cursor;
        import android.database.sqlite.SQLiteDatabase;
        import android.util.Log;

        public class ReminderBootUp extends BroadcastReceiver {
            private SQLiteDatabase myDataBase = null;
            private Cursor rs = null;

            @Override
            public void onReceive(Context context, Intent arg1) {
                // TODO Auto-generated method stub
                try {
                    long alarmTime = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(
                            "2010-12-21 12:00").getTime();
                    int id = 1;
                    Date date = new Date();
                    if (date.getTime() <= alarmTime) {
                        Intent notifyIntent = new Intent(context, youractivity.class);
                        PendingIntent intent = PendingIntent.getBroadcast(context, id,
                                notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
                        AlarmManager am = (AlarmManager) context
                                .getSystemService(Context.ALARM_SERVICE);
                        am.set(AlarmManager.RTC_WAKEUP, alarmTime, intent);
                    }

                } catch (Exception e) {
                    Log.e("Exception", e.getMessage(), e);
                }
            }

        }

you should add this line in Manifest.xml file

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED">
<receiver android:name="ReminderBootUp" android:permission="android.permission.RECEIVE_BOOT_COMPLETED" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
<category android:name="android.intent.category.DEFAULT"></category>
</intent-filter>
</receiver>

this code will work after booting the device...

if you want to start your application as normal statup activity, write as our normal code

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜