当前位置: 首页 > 知识库问答 >
问题:

Android在IntentService内部创建意图

微生弘
2023-03-14

我对Android系统非常陌生,我一直在用头撞墙,我认为这可能是一个微不足道的问题——我已经看过教程和其他StackOverflow问题,但无济于事。

我试图在IntentService中创建一个Intent,但在下面的行标记错误(在sendNotification方法中)上不断得到NullPointerException。

我尝试过不同的方法:

意向意向=新意向(getApplicationContext(),TestActivity)。阶级);

Intent Intent=新的Intent(这是TestActivity.class);

Intent Intent=new Intent(SQLHealthService.this,TestActivity.class);

无济于事,总是产生空指针异常时,试图创建Intent。

    public class SQLHealthService extends IntentService {
.
.
.
    public SQLHealthService(String url) {
        super("SQLHealth Service");
        // TODO Auto-generated constructor stub
        this.wsURL = "http://demo9138157.mockable.io/";
        serverList = new ArrayList<Server>();
    }

    @Override
    protected void onHandleIntent(Intent intent) {
    // TODO Auto-generated method stub
    Log.i(Constants.LOG_TAG, "SQLHealthService invoked");
    getServers();
    }

    List<Server> getServers() {
    AsyncHttpClient client = new AsyncHttpClient();
    client.get(wsURL, new AsyncHttpResponseHandler() {
        @Override
        public void onSuccess(String response) {
            try {
                .
                                    .
                                    .
                    sendNotification(notify);
            } catch (Exception e) {
                System.out.println("Could not create json object");
                System.out.println(e);
            }

            // System.out.println(response);
        }
         });

         return serverList;
      }

    private void sendNotification(int notify) {
    try {

    Intent intent = new Intent(getApplicationContext(), TestActivity.class); <-- ERROR
    intent.putExtra(Constants.FORCE_RELOAD, true);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            intent, 0);

    Notification mNotification = new Notification.Builder(this)
    .setContentTitle("SQL Server issues detected")
    .setContentText(notify + " SQL Servers with issues have been detected")
    .setSmallIcon(R.drawable.database)
    .setContentIntent(contentIntent)
    .addAction(R.drawable.erase_database, "View", contentIntent)
    .addAction(0, "Remind", contentIntent)
    .build();

    NotificationManager mgr = (NotificationManager) this
            .getSystemService(Context.NOTIFICATION_SERVICE);

    mNotification.flags |= Notification.FLAG_AUTO_CANCEL;

    mgr.notify(0, mNotification);
    } catch (Exception e) {

    System.out.println(e);
    }
}

编辑:为了清楚起见,我应该提到我正在努力实现的目标。我正在尝试使用AlarmManager让应用程序每10分钟“唤醒”,调用REST web服务,然后如果满足某些条件,则发送通知。

SQLHealthService是从

public class SQLHealthAlarmReceiver extends BroadcastReceiver {
      // onReceive must be very quick and not block, so it just fires up a Service
       @Override
       public void onReceive(Context context, Intent intent) {
          Log.i(Constants.LOG_TAG, "SQLHealthAlarmReceiver invoked, starting SQLHealthService");
          context.startService(new Intent(context, SQLHealthService.class));
       }
}

共有1个答案

鲁华皓
2023-03-14

IntentService不是为等待响应而设计的。一旦您从onHandleIntent返回,如果队列中不再有意图,服务就会被杀死(查看留档以了解这背后的推理)。因此,当您到达sendNotify时,不存在上下文。

 类似资料:
  • 致命异常:java.lang.nullpointerException:试图在com.myapp.apkofferQueueManagerIntentService.OnHandleIntent(SourceFile:71),android.app.IntentService$ServiceHandler.HandleMessage(IntentService.java:65),android.o

  • 当用户按下发送“按钮1”(向下滚动查看应用程序的构造)时,将从创建一个新的。如果用户按下此通知,实例将启动,并在上接收具有值的。 将显示该值。 当用户立即按下发送“按钮2”时,将从创建一个新的。如果用户按下此通知,实例将启动,并接收一个,该字符串还带有的值,位于上。 获得第二个按钮值的唯一解决办法是重新启动电话并先按下第二个按钮。即使强行接近也不起作用。 我知道我也可以用另一种方式改变UI。但是我

  • 问题内容: 如何在method内部创建方法?当我创建其显示错误时: 令牌无效@上的语法错误 如果不能在方法内部创建方法,那么请告诉我如何在方法外部创建方法,并从方法中传递方法。 问题答案: *请注意,应使用没有不等号的实际类型(例如“ int”和“ short”)替换此类标记。

  • 我现在是这样做的: > 我有一个在后台运行并读取用户位置的服务。每次读取一个有效的位置(有一些参数,如距离和时间),一个IntentService开始将该位置发送到网络服务器 使用追踪服务的应用程序也有一些网络通话,具体取决于用户按下的选项。现在,应用程序只是在异步任务中调用web服务。 一些代码:位置服务会在每次收到好位置时启动IntentService,如下所示: intentservice处

  • 我正在遵循那个教程(https://www.baeldung.com/spring-boot-minikube)我想在yaml文件(simple-crud-dpl.yaml)中创建Kubernetes部署: 但是当我运行<code>时,kubectl创建了一个-f简单的crud dpl。yaml我得到: 我使用的是最新版本的库贝特: 我也在本地使用minikube,正如教程中所描述的。部署和服务前

  • 我对IntentService的使用有点困惑。 文档中说,IntentService将发送给它的所有意图排队,并一次处理一个意图 我很确定我在文档中读到过这样的情况:系统只调用onStartCommand()一次,如果您发出两次startService(),第二次调用不会导致调用onStartCommand()<我可能错了,因为我一直在寻找这篇文档,但似乎找不到 这与之前的概念相矛盾,即可以通过o