gpt4 book ai didi

java - 非 Activity 类的Android通知

转载 作者:行者123 更新时间:2023-12-02 03:41:15 25 4
gpt4 key购买 nike

我想从另一个类中调用MainActivity中的notify()方法。这是我的方法:

public class MyAlarmService extends Service{
@Override
public IBinder onBind(Intent arg0)
{
// TODO Auto-generated method stub
return null;
}

@Override
public void onCreate()
{
// TODO Auto-generated method stub
super.onCreate();
}

@SuppressWarnings("static-access")
@Override
public void onStart(Intent intent, int startId)
{
super.onStart(intent, startId);

String title = "title";
String message = "message";

new MainActivity().Notify(title, message);
...


主要活动:

public class MainActivity extends Activity {
NotificationManager manager;
Notification myNotication;

@Override

protected void onCreate(Bundle savedInstanceState) {
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

...
public void Notify(String notificationTitle, String notificationMessage){
Intent intent = new Intent("com.xxxx.app.MainActivity");

PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 1, intent, 0);

Notification.Builder builder = new Notification.Builder(MainActivity.this);

builder.setAutoCancel(false);
builder.setTicker("this is ticker text");
builder.setContentTitle("Notification");
builder.setContentText("Text");
builder.setSmallIcon(R.drawable.infoicon);
builder.setContentIntent(pendingIntent);
builder.setOngoing(true);
builder.setSubText("This is subtext...");
builder.setNumber(100);
builder.build();
myNotication = builder.getNotification();
manager.notify(11, myNotication);
}

...


但是我收到一条错误消息:

致命异常:主要
                                                                                                    流程:com.xxxx.app,PID:10311
                                                                                                    java.lang.RuntimeException:无法使用意图{cmp = com.xxxx.app / .MyAlarmService}启动服务com.xxxx.app.MyAlarmService@636e513:java.lang.NullPointerException:尝试调用虚拟方法'java.lang。空对象引用上的字符串android.content.Context.getPackageName()'
                                                                                                        在android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3027)
                                                                                                        在android.app.ActivityThread.-wrap17(ActivityThread.java)
                                                                                                        在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1442)
                                                                                                        在android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                                        在android.os.Looper.loop(Looper.java:148)
                                                                                                        在android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                                        在java.lang.reflect.Method.invoke(本机方法)
                                                                                                        在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                                        在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                                                     原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'java.lang.String android.content.Context.getPackageName()'
                                                                                                        在android.content.ContextWrapper.getPackageName(ContextWrapper.java:133)
                                                                                                        在android.app.PendingIntent.getActivity(PendingIntent.java:305)
                                                                                                        在android.app.PendingIntent.getActivity(PendingIntent.java:272)
                                                                                                        在com.xxxx.app.MainActivity.Notify(MainActivity.java:304)
                                                                                                        在com.xxxx.app.MyAlarmService.onStart(MyAlarmService.java:41)
                                                                                                        在android.app.Service.onStartCommand(Service.java:459)
                                                                                                        在android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3010)
                                                                                                        在android.app.ActivityThread.-wrap17(ActivityThread.java)
                                                                                                        在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1442)
                                                                                                        在android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                                        在android.os.Looper.loop(Looper.java:148)
                                                                                                        在android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                                        在java.lang.reflect.Method.invoke(本机方法)
                                                                                                        在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                                        在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

最佳答案

您可以通过以下方式进行相同操作:

1)将那个Notify方法复制到服务中,并在需要顺序触发通知的任何地方调用它,Notification Service需要活动或服务的上下文)

2)您可以通过广播接收器从服务与活动进行通信

3)您可以使用aidl做

关于java - 非 Activity 类的Android通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36813583/

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