如何在android中自动调用我的应用程序

发布于 2024-10-08 14:29:23 字数 49 浏览 3 评论 0原文

我想在凌晨12点自动调用我的应用程序,这种过程,如何在android中实现,可能吗?

I want to automatically invoke my application at 12.00 am,This kind of process ,how to implement in android,is it possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只是一片海 2024-10-15 14:29:23

查看 AlarmManager

这是一个例子:

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);

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);
却一份温柔 2024-10-15 14:29:23

ReminderBootUp.java 文件包含在特定时间安排您的活动的代码

     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);
                }
            }

        }

,您应该在 Manifest.xml 文件中添加此行,

<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>

此代码将在启动设备后工作...

如果您想像正常的启动活动一样启动应用程序,
像我们正常的代码一样写

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文