gpt4 book ai didi

java - 如何从 JobScheduler 的 onStartJob 打开带有 ACTION_VIEW Intent 的 url

转载 作者:行者123 更新时间:2023-11-29 04:26:10 26 4
gpt4 key购买 nike

我正在尝试使用 JobScheduler ServiceonStartJob 方法中的 ACTION_VIEW Intent 在浏览器中打开一个 url >。下面是代码:

主要 Activity

package com.rohitkhatri.jobscheduler;

import android.app.job.JobInfo;
import android.app.job.JobScheduler;
import android.content.ComponentName;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

import com.rohitkhatri.jobscheduler.MyService;

public class MainActivity extends AppCompatActivity {
final int JOB_ID = 101;
JobScheduler jobScheduler;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
jobScheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);

scheduleJob();
}

public void scheduleJob() {
ComponentName componentName = new ComponentName(this, MyService.class);
JobInfo.Builder jobInfo = new JobInfo.Builder(JOB_ID, componentName);
jobInfo.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);

int response = jobScheduler.schedule(jobInfo.build());

if (response == JobScheduler.RESULT_FAILURE) {
Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG).show();
}
}
}

我的服务

package com.rohitkhatri.jobscheduler;

import android.app.job.JobService;
import android.content.Intent;
import android.net.Uri;
import android.widget.Toast;

public class MyService extends JobService {
@Override
public boolean onStartJob(JobParameters jobParameters) {
Toast.makeText(this, "Job executed", Toast.LENGTH_SHORT).show();

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://google.com"));
startActivity(intent);
return false;
}

@Override
public boolean onStopJob(JobParameters jobParameters) {
Toast.makeText(this, "Job Stopped", Toast.LENGTH_SHORT).show();
return false;
}
}

安卓 list

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rohitkhatri.jobscheduler">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<service android:name=".MyService"
android:permission="android.permission.BIND_JOB_SERVICE"
android:enabled="true"
android:exported="true"/>
</application>

</manifest>

但我收到以下错误:

09-30 20:57:31.486 30106-30106/com.rohitkhatri.jobscheduler E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.rohitkhatri.jobscheduler, PID: 30106
java.lang.RuntimeException: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
at android.app.job.JobService$JobHandler.handleMessage(JobService.java:130)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Caused by: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
at android.app.ContextImpl.startActivity(ContextImpl.java:672)
at android.app.ContextImpl.startActivity(ContextImpl.java:659)
at android.content.ContextWrapper.startActivity(ContextWrapper.java:331)
at com.rohitkhatri.jobscheduler.MyService.onStartJob(MyService.java:23)
at android.app.job.JobService$JobHandler.handleMessage(JobService.java:126)
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5443) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 

最佳答案

您需要设置 FLAG_ACTIVITY_NEW_TASK 标志以便从 Activity 上下文外部调用 startActivity()

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://google.com"));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

关于java - 如何从 JobScheduler 的 onStartJob 打开带有 ACTION_VIEW Intent 的 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46142401/

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