您好我试图从ScreenReceiver类中调用mainactivity中的方法。我找到了这个并将其实现到我的代码中。
MainActivity:
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// ScreenRecv
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mReceiver = new ScreenReceiver();
registerReceiver(mReceiver, filter);
}
// Toast(String)
public void Toast(String text) {
Toast.makeText(MainActivity.this, "" + text, Toast.LENGTH_SHORT).show();
}
}
ScreenReceiver:
package com.wifitimer;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class ScreenReceiver extends BroadcastReceiver {
public static boolean wasScreenOn = true;
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
new MainActivity().Toast("Screen Off");
wasScreenOn = false;
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
new MainActivity().Toast("Screen On");
wasScreenOn = true;
}
}
}
我没有错,但Toast也没有显示。有什么想法吗?
logcat的:
04-16 00:53:35.470: I/WifiManager(19123): setWifiEnabled : true
04-16 00:53:57.135: D/AndroidRuntime(19123): Shutting down VM
04-16 00:53:57.135: W/dalvikvm(19123): threadid=1: thread exiting with uncaught exception (group=0x416922a0)
04-16 00:53:57.140: E/AndroidRuntime(19123): FATAL EXCEPTION: main
04-16 00:53:57.140: E/AndroidRuntime(19123): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.SCREEN_OFF flg=0x50000010 } in com.wifitimer.ScreenReceiver@42312238
04-16 00:53:57.140: E/AndroidRuntime(19123): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:765)
04-16 00:53:57.140: E/AndroidRuntime(19123): at android.os.Handler.handleCallback(Handler.java:615)
04-16 00:53:57.140: E/AndroidRuntime(19123): at android.os.Handler.dispatchMessage(Handler.java:92)
04-16 00:53:57.140: E/AndroidRuntime(19123): at android.os.Looper.loop(Looper.java:137)
04-16 00:53:57.140: E/AndroidRuntime(19123): at android.app.ActivityThread.main(ActivityThread.java:4898)
04-16 00:53:57.140: E/AndroidRuntime(19123): at java.lang.reflect.Method.invokeNative(Native Method)
04-16 00:53:57.140: E/AndroidRuntime(19123): at java.lang.reflect.Method.invoke(Method.java:511)
04-16 00:53:57.140: E/AndroidRuntime(19123): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
04-16 00:53:57.140: E/AndroidRuntime(19123): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
04-16 00:53:57.140: E/AndroidRuntime(19123): at dalvik.system.NativeStart.main(Native Method)
04-16 00:53:57.140: E/AndroidRuntime(19123): Caused by: java.lang.NullPointerException
04-16 00:53:57.140: E/AndroidRuntime(19123): at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:101)
04-16 00:53:57.140: E/AndroidRuntime(19123): at com.wifitimer.MainActivity.Toast(MainActivity.java:107)
04-16 00:53:57.140: E/AndroidRuntime(19123): at com.wifitimer.ScreenReceiver.onReceive(ScreenReceiver.java:13)
04-16 00:53:57.140: E/AndroidRuntime(19123): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:755)
04-16 00:53:57.140: E/AndroidRuntime(19123): ... 9 more
04-16 00:53:57.445: I/Process(19123): Sending signal. PID: 19123 SIG: 9
答案 0 :(得分:3)
您的通话实际上不起作用,因为您需要将Context
作为第一个参数提供给makeText
。而且由于你的类不是从Context或Activity派生出来的,所以它甚至不应该编译。
现在显示您的代码,您目前遇到的问题是:
new MainActivity().Toast("Screen Off");
您已经有一个实例化的MainActivity,您不需要实例化一个新实例。现在,如果你想使用第一个类的方法,你可以做的一件事是为你的ScreenReceiver添加一个构造函数
MainActivity myContext;
public ScreenReceiver(MainActivity context){
myContext = context;
}
并在onReceive方法中将该行更改为
myContext.Toast("Screen Off");
要更改类的实例,您需要将该行更改为
BroadcastReceiver mReceiver = new ScreenReceiver(this);
答案 1 :(得分:0)
创建Toast时,第一个参数需要是Context。因此,B需要是活动/服务/应用程序的子类,或者您需要将对其构造函数中的B的引用传递给有效的Context。或者,您可以子类化Application并提供静态getContext方法。
答案 2 :(得分:0)
您无法直接实例化活动。在A中使用 startActivity(this, B.class);
并从B生命周期方法中调用methodB。
编辑以匹配已修改的问题:
如果您只想展示Toast,请直接在BroadcastReceiver
中执行:
Toast.makeText(context, "Screen On/Off", Toast.LENGTH_SHORT).show();
如果要调用创建Activity
的组件(BroadcastReceiver
)上的函数,最好添加回调接口并在Activity
中实现,通过构造函数发送(如DigCamara在他的回答中说道,或者设置为BroadcastReceiver
并在需要时打电话。
注意:
Activities
和Services
。它根本不会工作。BroadcastReceiver
(在您的情况下为onDestroy
)。public static boolean wasScreenOn = true;
。在杀死并重新启动进程后,您将丢失此数据。