我有两个类,一个是HomeActivity,第二个是AlarmReciever
在HomeActivity中创建的AlarmManager服务,它在一段指定的时间内调用AlarmReciver
现在我想要在调用AlarmReciver时显示一个对话框,它应该是 在HomeActivity中定义
这是两个文件的源代码和LogCat
import android.os.Bundle;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.Dialog;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class HomeActivity extends Activity {
private TextView textView;
private PendingIntent pendingIntent;
private AlarmManager manager;
static Dialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Intent alarmIntent = new Intent(HomeActivity.this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);
manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
int interval = 10000;
manager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);
Toast.makeText(this, "Antivirus Started", Toast.LENGTH_SHORT).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_home, menu);
return true;
}
public void openDialog(){
dialog = new Dialog(HomeActivity.this);
dialog.setContentView(R.layout.ask_question);
}
}
//这是第二类
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class AlarmReceiver extends BroadcastReceiver {
Context c;
@Override
public void onReceive(Context arg0, Intent arg1) {
HomeActivity home = new HomeActivity();
home.openDialog();
}
}
//和LogCat
10-27 19:51:40.645: D/dalvikvm(27137): Late-enabling CheckJNI
10-27 19:51:41.545: E/DATABASE(27137): SELECT * FROM register
10-27 19:51:42.075: D/OpenGLRenderer(27137): Enabling debug mode 0
10-27 19:51:52.775: D/AndroidRuntime(27137): Shutting down VM
10-27 19:51:52.785: W/dalvikvm(27137): threadid=1: thread exiting with uncaught exception (group=0x41698c08)
10-27 19:51:52.795: E/AndroidRuntime(27137): FATAL EXCEPTION: main
10-27 19:51:52.795: E/AndroidRuntime(27137): Process: com.example.malwaredetection, PID: 27137
10-27 19:51:52.795: E/AndroidRuntime(27137): java.lang.RuntimeException: Unable to start receiver com.example.malwaredetection.AlarmReceiver: java.lang.NullPointerException
10-27 19:51:52.795: E/AndroidRuntime(27137): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2704)
10-27 19:51:52.795: E/AndroidRuntime(27137): at android.app.ActivityThread.access$1800(ActivityThread.java:179)
10-27 19:51:52.795: E/AndroidRuntime(27137): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1400)
10-27 19:51:52.795: E/AndroidRuntime(27137): at android.os.Handler.dispatchMessage(Handler.java:102)
10-27 19:51:52.795: E/AndroidRuntime(27137): at android.os.Looper.loop(Looper.java:146)
10-27 19:51:52.795: E/AndroidRuntime(27137): at android.app.ActivityThread.main(ActivityThread.java:5641)
10-27 19:51:52.795: E/AndroidRuntime(27137): at java.lang.reflect.Method.invokeNative(Native Method)
10-27 19:51:52.795: E/AndroidRuntime(27137): at java.lang.reflect.Method.invoke(Method.java:515)
10-27 19:51:52.795: E/AndroidRuntime(27137): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1288)
10-27 19:51:52.795: E/AndroidRuntime(27137): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1104)
10-27 19:51:52.795: E/AndroidRuntime(27137): at dalvik.system.NativeStart.main(Native Method)
10-27 19:51:52.795: E/AndroidRuntime(27137): Caused by: java.lang.NullPointerException
10-27 19:51:52.795: E/AndroidRuntime(27137): at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:166)
10-27 19:51:52.795: E/AndroidRuntime(27137): at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:103)
10-27 19:51:52.795: E/AndroidRuntime(27137): at android.app.Dialog.<init>(Dialog.java:159)
10-27 19:51:52.795: E/AndroidRuntime(27137): at android.app.Dialog.<init>(Dialog.java:136)
10-27 19:51:52.795: E/AndroidRuntime(27137): at com.example.malwaredetection.HomeActivity.openDialog(HomeActivity.java:87)
10-27 19:51:52.795: E/AndroidRuntime(27137): at com.example.malwaredetection.AlarmReceiver.onReceive(AlarmReceiver.java:15)
10-27 19:51:52.795: E/AndroidRuntime(27137): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2687)
10-27 19:51:52.795: E/AndroidRuntime(27137): ... 10 more
10-27 19:52:55.735: D/AndroidRuntime(27939): Shutting down VM
10-27 19:52:55.735: W/dalvikvm(27939): threadid=1: thread exiting with uncaught exception (group=0x41698c08)
10-27 19:52:55.745: E/AndroidRuntime(27939): FATAL EXCEPTION: main
10-27 19:52:55.745: E/AndroidRuntime(27939): Process: com.example.malwaredetection, PID: 27939
10-27 19:52:55.745: E/AndroidRuntime(27939): java.lang.RuntimeException: Unable to start receiver com.example.malwaredetection.AlarmReceiver: java.lang.NullPointerException
10-27 19:52:55.745: E/AndroidRuntime(27939): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2704)
10-27 19:52:55.745: E/AndroidRuntime(27939): at android.app.ActivityThread.access$1800(ActivityThread.java:179)
10-27 19:52:55.745: E/AndroidRuntime(27939): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1400)
10-27 19:52:55.745: E/AndroidRuntime(27939): at android.os.Handler.dispatchMessage(Handler.java:102)
10-27 19:52:55.745: E/AndroidRuntime(27939): at android.os.Looper.loop(Looper.java:146)
10-27 19:52:55.745: E/AndroidRuntime(27939): at android.app.ActivityThread.main(ActivityThread.java:5641)
10-27 19:52:55.745: E/AndroidRuntime(27939): at java.lang.reflect.Method.invokeNative(Native Method)
10-27 19:52:55.745: E/AndroidRuntime(27939): at java.lang.reflect.Method.invoke(Method.java:515)
10-27 19:52:55.745: E/AndroidRuntime(27939): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1288)
10-27 19:52:55.745: E/AndroidRuntime(27939): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1104)
10-27 19:52:55.745: E/AndroidRuntime(27939): at dalvik.system.NativeStart.main(Native Method)
10-27 19:52:55.745: E/AndroidRuntime(27939): Caused by: java.lang.NullPointerException
10-27 19:52:55.745: E/AndroidRuntime(27939): at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:166)
10-27 19:52:55.745: E/AndroidRuntime(27939): at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:103)
10-27 19:52:55.745: E/AndroidRuntime(27939): at android.app.Dialog.<init>(Dialog.java:159)
10-27 19:52:55.745: E/AndroidRuntime(27939): at android.app.Dialog.<init>(Dialog.java:136)
10-27 19:52:55.745: E/AndroidRuntime(27939): at com.example.malwaredetection.HomeActivity.openDialog(HomeActivity.java:87)
10-27 19:52:55.745: E/AndroidRuntime(27939): at com.example.malwaredetection.AlarmReceiver.onReceive(AlarmReceiver.java:15)
10-27 19:52:55.745: E/AndroidRuntime(27939): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2687)
10-27 19:52:55.745: E/AndroidRuntime(27939): ... 10 more
10-27 19:53:01.655: D/AndroidRuntime(28095): Shutting down VM
10-27 19:53:01.665: W/dalvikvm(28095): threadid=1: thread exiting with uncaught exception (group=0x41698c08)
10-27 19:53:01.735: E/AndroidRuntime(28095): FATAL EXCEPTION: main
10-27 19:53:01.735: E/AndroidRuntime(28095): Process: com.example.malwaredetection, PID: 28095
10-27 19:53:01.735: E/AndroidRuntime(28095): java.lang.RuntimeException: Unable to start receiver com.example.malwaredetection.AlarmReceiver: java.lang.NullPointerException
10-27 19:53:01.735: E/AndroidRuntime(28095): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2704)
10-27 19:53:01.735: E/AndroidRuntime(28095): at android.app.ActivityThread.access$1800(ActivityThread.java:179)
10-27 19:53:01.735: E/AndroidRuntime(28095): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1400)
10-27 19:53:01.735: E/AndroidRuntime(28095): at android.os.Handler.dispatchMessage(Handler.java:102)
10-27 19:53:01.735: E/AndroidRuntime(28095): at android.os.Looper.loop(Looper.java:146)
10-27 19:53:01.735: E/AndroidRuntime(28095): at android.app.ActivityThread.main(ActivityThread.java:5641)
10-27 19:53:01.735: E/AndroidRuntime(28095): at java.lang.reflect.Method.invokeNative(Native Method)
10-27 19:53:01.735: E/AndroidRuntime(28095): at java.lang.reflect.Method.invoke(Method.java:515)
10-27 19:53:01.735: E/AndroidRuntime(28095): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1288)
10-27 19:53:01.735: E/AndroidRuntime(28095): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1104)
10-27 19:53:01.735: E/AndroidRuntime(28095): at dalvik.system.NativeStart.main(Native Method)
10-27 19:53:01.735: E/AndroidRuntime(28095): Caused by: java.lang.NullPointerException
10-27 19:53:01.735: E/AndroidRuntime(28095): at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:166)
10-27 19:53:01.735: E/AndroidRuntime(28095): at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:103)
10-27 19:53:01.735: E/AndroidRuntime(28095): at android.app.Dialog.<init>(Dialog.java:159)
10-27 19:53:01.735: E/AndroidRuntime(28095): at android.app.Dialog.<init>(Dialog.java:136)
10-27 19:53:01.735: E/AndroidRuntime(28095): at com.example.malwaredetection.HomeActivity.openDialog(HomeActivity.java:87)
10-27 19:53:01.735: E/AndroidRuntime(28095): at com.example.malwaredetection.AlarmReceiver.onReceive(AlarmReceiver.java:15)
10-27 19:53:01.735: E/AndroidRuntime(28095): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2687)
10-27 19:53:01.735: E/AndroidRuntime(28095): ... 10 more
答案 0 :(得分:0)
首先,不要这样做:
HomeActivity home = new HomeActivity()
您无法手动实例化活动实例。相反,你应该让系统为你做这件事。如果您想start a new activity,请使用Intent
。
您可以通过不同的方式回答实际问题,具体取决于您的要求和所需的运行时行为。
例如,如果您只想在应用运行时显示对话框,则可以使用many 'event bus' implementations中的一个,或者只使用接收器设置自己的(local) broadcast manager。
如果您还需要显示对话框,即使您的应用不在前台(如果您决定沿着这条路走下去,也要好好考虑用户体验),您可以利用activity launch modes开始你的HomeActivity
。此方法将使您能够优雅地处理后台堆栈上已存在活动记录的场景(以及不存在的场景)。
答案 1 :(得分:0)
你不应该这样做:HomeActivity home = new HomeActivity();
因为活动是用意图创建的。
您可以这样做:
<强> AlarmReceiver:强>
@Override
public void onReceive(Context context, Intent intent) {
Intent homeActivityIntent = new Intent(context, HomeActivity.class);
homeActivityIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
homeActivityIntent.putExtra("value_name", "value_data");
context.startActivity(homeActivityIntent);
}
<强> HomeActivity:强>
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
String someData = intent.getStringExtra("value_name");
//Maybe check value of someData to decide
//weather to open the dialog or not
this.openDialog();
}