如何安排 Android 以后的意图?
How to schedule an intent for later in Android?
我刚刚开始 Android 开发并遇到了这个问题。我正在尝试在特定时间后启动 Intent。
我尝试通过将 Intent 放入 Runnable 来使用 runnable 和 handler.postAtTime 方法。当您销毁应用程序或重新启动 phone.
时,此方法不执行任何操作
我尝试设置 BroadcastReceiver,但它不允许我将 startActivity(Intent) 用于 Intent。我尝试使用 Context.startActivity(Intent),但如果应用程序在 Intent 完成之前被销毁,它就会停止工作。
谁能帮我解决这个问题?谢谢
编辑 1 :
这是我使用 AlarmManager 调用的 BroadcastReceiver:
package com.mallock.sch_email2;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.widget.Toast;
public class Broadcast extends BroadcastReceiver{
@Override
public void onReceive(Context arg0, Intent arg1) {
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, Activity1.subj);
intent.putExtra(Intent.EXTRA_TEXT, Activity1.body);
intent.setData(Uri.parse("mailto:".concat(Activity1.rec)));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(arg0,intent);
Toast.makeText(arg0, "I'm running", Toast.LENGTH_SHORT).show();
}
}
logcat:
07-15 21:43:13.980 4569-4569/com.mallock.sch_email2 E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.mallock.sch_email2, PID: 4569
java.lang.RuntimeException: Unable to start receiver com.mallock.sch_email2.Broadcast: java.lang.NullPointerException
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2580)
at android.app.ActivityThread.access00(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at java.lang.String.concat(String.java:678)
at com.mallock.sch_email2.Broadcast.onReceive(Broadcast.java:16)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2573)
at android.app.ActivityThread.access00(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
这个class适合你! AlarmManager 是正确的方法。
要在将来的特定时间执行事件,您需要使用 AlarmManager class .
例如,如果您想在 2 小时后打开 activity,则:
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, MyReciever.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
am.set( AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 7200000 , pendingIntent );
接收者::::
public class MyReciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent intent = new Intent(context, MyActivity.class);
intent.addFlags(
Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
}
}
谢谢
我正在使用 PendingIntent.getBroadcast 然后使用 BroadcastReceiver 启动一个 Intent。直接使用 PendingIntent.getActivity 解决了这个问题。
@Mimmo Grottoli:好的...谢谢 :)
我刚刚开始 Android 开发并遇到了这个问题。我正在尝试在特定时间后启动 Intent。
我尝试通过将 Intent 放入 Runnable 来使用 runnable 和 handler.postAtTime 方法。当您销毁应用程序或重新启动 phone.
时,此方法不执行任何操作
我尝试设置 BroadcastReceiver,但它不允许我将 startActivity(Intent) 用于 Intent。我尝试使用 Context.startActivity(Intent),但如果应用程序在 Intent 完成之前被销毁,它就会停止工作。
谁能帮我解决这个问题?谢谢
编辑 1 :
这是我使用 AlarmManager 调用的 BroadcastReceiver:
package com.mallock.sch_email2;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.widget.Toast;
public class Broadcast extends BroadcastReceiver{
@Override
public void onReceive(Context arg0, Intent arg1) {
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, Activity1.subj);
intent.putExtra(Intent.EXTRA_TEXT, Activity1.body);
intent.setData(Uri.parse("mailto:".concat(Activity1.rec)));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(arg0,intent);
Toast.makeText(arg0, "I'm running", Toast.LENGTH_SHORT).show();
}
}
logcat:
07-15 21:43:13.980 4569-4569/com.mallock.sch_email2 E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.mallock.sch_email2, PID: 4569
java.lang.RuntimeException: Unable to start receiver com.mallock.sch_email2.Broadcast: java.lang.NullPointerException
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2580)
at android.app.ActivityThread.access00(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at java.lang.String.concat(String.java:678)
at com.mallock.sch_email2.Broadcast.onReceive(Broadcast.java:16)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2573)
at android.app.ActivityThread.access00(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
这个class适合你! AlarmManager 是正确的方法。
要在将来的特定时间执行事件,您需要使用 AlarmManager class .
例如,如果您想在 2 小时后打开 activity,则:
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, MyReciever.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
am.set( AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 7200000 , pendingIntent );
接收者::::
public class MyReciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent intent = new Intent(context, MyActivity.class);
intent.addFlags(
Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
}
}
谢谢
我正在使用 PendingIntent.getBroadcast 然后使用 BroadcastReceiver 启动一个 Intent。直接使用 PendingIntent.getActivity 解决了这个问题。
@Mimmo Grottoli:好的...谢谢 :)