4

特定のWebサイトを1分ごとにチェックして、探しているものが見つかるかどうかを確認し、アイテムが見つかるたびに通知(Plays Sound)するアプリがあります。このツタンカーメンに従ってアプリをバックグラウンドで実行しましたが、WebViewについて不平を言っていることに気づきました。

http://marakana.com/forums/android/examples/60.html

サービス内でWebViewを使用できない場合、同じ目標を達成するための代替手段は何ですか?

4

3 に答える 3

1

はい、サービスはバックグラウンドで実行され、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);
于 2013-03-16T22:28:18.310 に答える
0

いいえ、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;
}
于 2013-03-16T22:24:53.403 に答える
-1

はい、プロジェクトの別のアクティビティから静的Webビューを使用できます。例:MainActivity.web2.loadUrl( ""); あなたのサービスで。

于 2020-07-20T04:29:24.113 に答える