特定のWebサイトを1分ごとにチェックして、探しているものが見つかるかどうかを確認し、アイテムが見つかるたびに通知(Plays Sound)するアプリがあります。このツタンカーメンに従ってアプリをバックグラウンドで実行しましたが、WebViewについて不平を言っていることに気づきました。
http://marakana.com/forums/android/examples/60.html
サービス内でWebViewを使用できない場合、同じ目標を達成するための代替手段は何ですか?
特定のWebサイトを1分ごとにチェックして、探しているものが見つかるかどうかを確認し、アイテムが見つかるたびに通知(Plays Sound)するアプリがあります。このツタンカーメンに従ってアプリをバックグラウンドで実行しましたが、WebViewについて不平を言っていることに気づきました。
http://marakana.com/forums/android/examples/60.html
サービス内でWebViewを使用できない場合、同じ目標を達成するための代替手段は何ですか?
はい、サービスはバックグラウンドで実行され、UIを表示できないはずです。
ただし、PendingIntent.getService(context、GET_ADSERVICE_REQUEST_CODE、...)を使用して、アクティビティ(UIプロセス)にそのコンテキストをサービスに渡すことができます。次に、サービスを表示する準備ができたら、以下の行でブラウザー(または独自のWebView用の適切なインテントフィルターを備えた所有者アプリ)を起動して、Webコンテンツを表示する必要があります。
Intent i = new Intent(Intent.ACTION_VIEW, url);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, i,
Intent.FLAG_ACTIVITY_NEW_TASK);
いいえ、WebView
サービス内で使用するべきではありません。とにかく、それは実際には意味がありません。含まれているhtmlをスクレイピングする目的でロードWebView
している場合は、次のようにHttpGetリクエストを実行することもできます-
public static String readFromUrl( String url ) {
String result = null;
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet( url );
HttpResponse response;
try {
response = client.execute( get );
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream is = entity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader( is ) );
StringBuilder sb = new StringBuilder();
String line = null;
try {
while( ( line = reader.readLine() ) != null )
sb.append( line + "\n" );
} catch ( IOException e ) {
Log.e( "readFromUrl", e.getMessage() );
} finally {
try {
is.close();
} catch ( IOException e ) {
Log.e( "readFromUrl", e.getMessage() );
}
}
result = sb.toString();
is.close();
}
} catch( Exception e ) {
Log.e( "readFromUrl", e.getMessage() );
}
return result;
}
はい、プロジェクトの別のアクティビティから静的Webビューを使用できます。例:MainActivity.web2.loadUrl( ""); あなたのサービスで。