gpt4 book ai didi

android - 从 BroadcastReceiver 调用 Activity 内部的方法

转载 作者:行者123 更新时间:2023-11-29 14:20:44 25 4
gpt4 key购买 nike

我正在使用 Parse.com 服务发送推送通知,并有一个广播接收器来接收消息。我想在我的 Activity 中从广播接收器调用一个方法来显示 Toast 消息(实际上我想更新 ListView 内容,但现在我只是使用 Toast 来检查方法是否被成功调用)而不调用 Activity 。

首先我按照这个instruction :

但它会产生错误:

12-20 20:53:33.892: E/AndroidRuntime(14245): FATAL EXCEPTION: main
12-20 20:53:33.892: E/AndroidRuntime(14245): java.lang.RuntimeException: Unable to start receiver event.planner.services.CustomReceiver: java.lang.NullPointerException
12-20 20:53:33.892: E/AndroidRuntime(14245): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2236)
12-20 20:53:33.892: E/AndroidRuntime(14245): at android.app.ActivityThread.access$1500(ActivityThread.java:130)
12-20 20:53:33.892: E/AndroidRuntime(14245): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1271)
12-20 20:53:33.892: E/AndroidRuntime(14245): at android.os.Handler.dispatchMessage(Handler.java:99)
12-20 20:53:33.892: E/AndroidRuntime(14245): at android.os.Looper.loop(Looper.java:137)
12-20 20:53:33.892: E/AndroidRuntime(14245): at android.app.ActivityThread.main(ActivityThread.java:4745)
12-20 20:53:33.892: E/AndroidRuntime(14245): at java.lang.reflect.Method.invokeNative(Native Method)
12-20 20:53:33.892: E/AndroidRuntime(14245): at java.lang.reflect.Method.invoke(Method.java:511)
12-20 20:53:33.892: E/AndroidRuntime(14245): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-20 20:53:33.892: E/AndroidRuntime(14245): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-20 20:53:33.892: E/AndroidRuntime(14245): at dalvik.system.NativeStart.main(Native Method)
12-20 20:53:33.892: E/AndroidRuntime(14245): Caused by: java.lang.NullPointerException
12-20 20:53:33.892: E/AndroidRuntime(14245): at event.planner.services.CustomReceiver.onReceive(CustomReceiver.java:60)
12-20 20:53:33.892: E/AndroidRuntime(14245): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2229)
12-20 20:53:33.892: E/AndroidRuntime(14245): ... 10 more
12-20 20:53:35.883: I/Process(14245): Sending signal. PID: 14245 SIG: 9
12-20 20:53:41.322: E/Trace(15934): error opening trace file: No such file or directory (2)
12-20 20:53:41.615: E/com.parse.PushService(15934): The Parse push service cannot start because Parse.initialize has not yet been called. If you call Parse.initialize from an Activity's onCreate, that call should instead be in the Application.onCreate. Be sure your Application class is registered in your AndroidManifest.xml with the android:name property of your <application> tag.

然后我尝试使用描述的静态方法 here ,它会产生以下错误:

12-20 20:18:49.553: E/AndroidRuntime(11779): FATAL EXCEPTION: main
12-20 20:18:49.553: E/AndroidRuntime(11779): java.lang.RuntimeException: Error receiving broadcast Intent { act=com.example.UPDATE_STATUS flg=0x10 (has extras) } in event.planner.services.CustomReceiver@41036bd0
12-20 20:18:49.553: E/AndroidRuntime(11779): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:765)
12-20 20:18:49.553: E/AndroidRuntime(11779): at android.os.Handler.handleCallback(Handler.java:615)
12-20 20:18:49.553: E/AndroidRuntime(11779): at android.os.Handler.dispatchMessage(Handler.java:92)
12-20 20:18:49.553: E/AndroidRuntime(11779): at android.os.Looper.loop(Looper.java:137)
12-20 20:18:49.553: E/AndroidRuntime(11779): at android.app.ActivityThread.main(ActivityThread.java:4745)
12-20 20:18:49.553: E/AndroidRuntime(11779): at java.lang.reflect.Method.invokeNative(Native Method)
12-20 20:18:49.553: E/AndroidRuntime(11779): at java.lang.reflect.Method.invoke(Method.java:511)
12-20 20:18:49.553: E/AndroidRuntime(11779): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-20 20:18:49.553: E/AndroidRuntime(11779): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-20 20:18:49.553: E/AndroidRuntime(11779): at dalvik.system.NativeStart.main(Native Method)
12-20 20:18:49.553: E/AndroidRuntime(11779): Caused by: java.lang.NullPointerException
12-20 20:18:49.553: E/AndroidRuntime(11779): at android.widget.Toast.<init>(Toast.java:92)
12-20 20:18:49.553: E/AndroidRuntime(11779): at android.widget.Toast.makeText(Toast.java:238)
12-20 20:18:49.553: E/AndroidRuntime(11779): at event.planner.front.FrontLayout.UpdateList(FrontLayout.java:193)
12-20 20:18:49.553: E/AndroidRuntime(11779): at event.planner.services.CustomReceiver.onReceive(CustomReceiver.java:60)
12-20 20:18:49.553: E/AndroidRuntime(11779): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:755)
12-20 20:18:49.553: E/AndroidRuntime(11779): ... 9 more
12-20 20:18:52.613: I/Process(11779): Sending signal. PID: 11779 SIG: 9
12-20 20:18:58.762: E/Trace(12109): error opening trace file: No such file or directory (2)
12-20 20:18:59.603: E/com.parse.PushService(12109): The Parse push service cannot start because Parse.initialize has not yet been called. If you call Parse.initialize from an Activity's onCreate, that call should instead be in the Application.onCreate. Be sure your Application class is registered in your AndroidManifest.xml with the android:name property of your <application> tag.

如何正确地做到这一点?谢谢。

结合这两种方法后我的代码:

CustomReceiver.java

public class CustomReceiver extends BroadcastReceiver {
private static final String TAG = "MyCustomReceiver";
public static final String ACTION = "com.example.UPDATE_STATUS";

FrontLayout main = null;
public void setMainActivityHandler(FrontLayout main){
this.main=main;
}

@Override
public void onReceive(Context context, Intent intent) {
String title="";
String alert="";
try {
JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));

Iterator<?> itr = json.keys();
while (itr.hasNext()) {
String key = (String) itr.next();
if(key.equals("title"))
title = json.getString(key);
if(key.equals("alert"))
alert = json.getString(key);
else
continue;
}
} catch (JSONException e) {
Log.w(TAG, "JSONException: " + e.getMessage());
}

SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss",Locale.US);
Date date = new Date();

NotificationModel nm = new NotificationModel();
nm.setTitle(title);
nm.setAlert(alert);
nm.setDateTime(dateFormat.format(date));

NotificationDS nds = new NotificationDS(context);
nds.insertNotification(nm);

main.UpdateList();
}
}

AndroidManifest.xml

<application>
.....
<service android:name="com.parse.PushService" />
<receiver android:name="com.parse.ParseBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>

<receiver android:name="event.planner.services.CustomReceiver"
android:exported="false">
<intent-filter>
<action android:name="com.example.UPDATE_STATUS" />
</intent-filter>
</receiver>
</application>
<application android:name="event.planner.services.MyApplication">
</application>

FrontLayout.java

public class FrontLayout extends Activity 
{
....
CustomReceiver receiver = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
......

receiver = new CustomReceiver();
receiver.setMainActivityHandler(this);
IntentFilter callInterceptorIntentFilter = new IntentFilter("com.example.UPDATE_STATUS");
registerReceiver(receiver, callInterceptorIntentFilter);

}

public static void UpdateList()
{
Toast.makeText(MyApplication.getAppContext(), "Success!", Toast.LENGTH_SHORT).show();
}
}

MyApplication.java

package event.planner.services;

import android.app.Application;
import android.content.Context;

public class MyApplication extends Application{

private static Context context;

public void onCreate(){
super.onCreate();
MyApplication.context = getApplicationContext();
}

public static Context getAppContext() {
return MyApplication.context;
}
}

最佳答案

按照这些步骤

Step 1:Declare a Interface class in that BroadcastReceiver

Step 2: Implement that interface in your Activity class...

示例代码:

在 CustomReceiver.class 内部------------

public interface Listener {
public void onUIChanges();
}

public void setListener(Listener listener) {
CustomReceiver .listener = listener;
}

@Override
public void onReceive(Context context, Intent intent)
{
-------
------

if(listener!=null)

{
listener.onUIChanges();
}
}

在 FrontLayout.class 内部

CustomReceiver.setListener(this);  // otherwise null pointer occurs

最后实现那个监听器...

关于android - 从 BroadcastReceiver 调用 Activity 内部的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20705691/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com