我正在尝试使用ACTION_VIEW onStartJobJobScheduler方法中的Service目的在浏览器中打开URL。下面是代码:

主要活动

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;
    }
}


Android清单

<?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标志才能从活动上下文外部调用startActivity()

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

10-04 10:41