java - How to open url with ACTION_VIEW intent from onStartJob of JobScheduler -
i trying open url in browser using action_view
intent onstartjob
method of jobscheduler
service
. below code:
mainactivity
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(); } } }
myservice
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; } }
androidmanifest
<?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>
but i'm getting following error:
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() outside of activity context requires flag_activity_new_task flag. want? @ android.app.job.jobservice$jobhandler.handlemessage(jobservice.java:130) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:148) @ android.app.activitythread.main(activitythread.java:5443) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:728) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:618) caused by: android.util.androidruntimeexception: calling startactivity() outside of activity context requires flag_activity_new_task flag. want? @ android.app.contextimpl.startactivity(contextimpl.java:672) @ android.app.contextimpl.startactivity(contextimpl.java:659) @ android.content.contextwrapper.startactivity(contextwrapper.java:331) @ com.rohitkhatri.jobscheduler.myservice.onstartjob(myservice.java:23) @ android.app.job.jobservice$jobhandler.handlemessage(jobservice.java:126) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:148) @ android.app.activitythread.main(activitythread.java:5443) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:728) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:618)
you need set flag_activity_new_task flag in order call startactivity() outside of activity context
intent intent = new intent(intent.action_view, uri.parse("https://google.com")); intent.setflags(intent.flag_activity_new_task); startactivity(intent);
Comments
Post a Comment