我正在尝试使用ACTION_VIEW
onStartJob
JobScheduler
Service
方法中的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();
}
}
}
意图在浏览器中打开网址。以下是代码:
MainActivity
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;
}
}
为MyService
<?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>
AndroidManifest
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)
但我收到以下错误:
FROM openjdk:8-jre-alpine
WORKDIR /myworkdir
COPY path/tomyproject/src/main/bin/start /myworkdir/start
...
EXPOSE 8080
CMD [ "sh", "/myworkdir/start" ]
答案 0 :(得分:2)
您需要设置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);
答案 1 :(得分:1)
如上所述,无处不在地启动UI是糟糕的用户体验,打断了用户正在做的任何事情,并可能导致他们误操作。您应该发布适当的通知。