尝试在设备连接到互联网时打开网页。但是,我使用的服务正在崩溃应用程序。 以下是Service类的源代码:
public class Opener extends Service {
final byte flag=0;
byte logger=0;
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
while(flag==0){
ConnectivityManager cm =
(ConnectivityManager)this.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&
activeNetwork.isConnectedOrConnecting();
if(isConnected)
{
if(logger==10)
{
this.stopSelf();
}
++logger;
Intent blower = new Intent();
blower.setAction(Intent.ACTION_VIEW);
blower.addCategory(Intent.CATEGORY_BROWSABLE);
blower.setData(Uri.parse("http://www.developer.android.com"));
startActivity(blower);
}
else{
;
}
}
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
}
这也是Logcat:
> Process: antivirus.dc.lab.antivirus, PID: 2264
java.lang.RuntimeException: Unable to start service antivirus.dc.lab.antivirus.Opener@21f96590 with Intent { cmp=antivirus.dc.lab.antivirus/.Opener }: 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.ActivityThread.handleServiceArgs(ActivityThread.java:2881)
at android.app.ActivityThread.access$2100(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1376)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
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:1232)
at android.app.ContextImpl.startActivity(ContextImpl.java:1219)
at android.content.ContextWrapper.startActivity(ContextWrapper.java:322)
at antivirus.dc.lab.antivirus.Opener.onStart(Opener.java:51)
at android.app.Service.onStartCommand(Service.java:458)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2864)
at android.app.ActivityThread.access$2100(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1376)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
从一个类似的问题,找到使用FLAG_ACTIVITY_NEW_TASK
的答案然而在我的情况下不起作用。任何帮助将不胜感激。
答案 0 :(得分:1)
您已将自己的意图命名为blower
,但是您将intent
传递给startActivity
。它应该是
startActivity(blower);