2013-08-30 47 views
0

我試圖從點擊一個按鈕的按鈕打開瀏覽器。我用了BroadcastReceiver,其中從onReceiver()我想打開一個瀏覽器。從一個按鈕點擊Widget打開瀏覽器

我有錯誤,如:

Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want? 
+0

可能重複http://stackoverflow.com/questions/5949746/is-it-not -post-to-start-an-activity-from-broadcastreceiver) – CommonsWare

+0

您是否在按鈕上設置了PendingIntent? –

回答

1

如果啓動和活動形成類BroadcaseReceiver一個無形的組成部分,你需要使用FLAG_ACTIVITY_NEW_TASK標誌。這是錯誤說的。這是一個可能的解決方案。

Intent intent = new Intent(<Your action here>); 
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
context.startActivity(intent); 

或者,您可以先顯示通知。這隻會在狀態欄中顯示一條消息,並且不會中斷當前用戶的活動(例如遊戲)。之後,當用戶有時間時,他或她可以點擊該通知,然後瀏覽器將被打開。

-2
button = (Button)findViewById(R.id.xxxx); 
button.setOnClickListener(View.OnClickListener){ 

public void onClick(){ 

String url = "http://www.example.com"; 
Intent i = new Intent(Intent.ACTION_VIEW); 
i.setData(Uri.parse(url)); 
startActivity(i); 

} 
} 
+0

如果這是在Activity中完成的,那麼您的答案就可以工作,但它是一個使用AppWidgetProvider的小部件。 – Finnboy11

0

希望這有助於--->

Button1 = (Button) findViewById(R.id.b1); 

String text ="Visit <a href=\"http://manning.com/\">Manning home page</a>"; 

Button1.setText(Html.fromHtml(text)); 
Button1.setMovementMethod(LinkMovementMethod.getInstance()); 
的[是沒可能開始從廣播接收器的活動(
相關問題