0

我正在尝试在我的书签/浏览器历史记录中搜索特定 URL,然后在找到匹配项后开始新的意图。问题是 - 新意图永远不会启动 - 尽管浏览器历史记录和书签都包含正在查询的值。

任何想法为什么会发生这种情况?

import com.parse.ParseObject; 
import android.app.AlertDialog;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.TrafficStats;
import android.net.Uri;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.provider.Browser;
import android.util.Log;
import android.widget.Chronometer;
import android.widget.TextView;
import android.widget.Toast;

public class Service_class extends Service {
       String Dirty1 = "www.playboy.com";
        String Dirty2 = "www.penthouse.com";
        String Dirty3 = "www.pornhub.com";
        String Dirty4 = "www.playboy.com";
        String Dirty5 = "www.playboy.com";
        String Dirty6 = "www.playboy.com";
        String Dirty7 = "www.playboy.com";
        String Dirty8 = "www.playboy.com";
        String Dirty9 = "www.playboy.com";
        String Dirty10 = "www.playboy.com";

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
        String[] projection = new String[] { Browser.BookmarkColumns.TITLE,
                Browser.BookmarkColumns.URL };
                Cursor cursor = getContentResolver().query(android.provider.Browser.BOOKMARKS_URI,
                        projection, null, null, null);
                String urls = "";
                if (cursor.moveToFirst()) {
                String url1 = null;
                String url2 = null;
                do {
                String url = cursor.getString(cursor.getColumnIndex(Browser.BookmarkColumns.URL));

                if (url.toLowerCase().contains(Dirty1)) {
                } else if (url.toLowerCase().contains(Dirty2)) {
                } else if (url.toLowerCase().contains(Dirty3)) {
                } else if (url.toLowerCase().contains(Dirty4)) {
                } else if (url.toLowerCase().contains(Dirty5)) {
                } else if (url.toLowerCase().contains(Dirty6)) {
                } else if (url.toLowerCase().contains(Dirty7)) {
                } else if (url.toLowerCase().contains(Dirty8)) {
                } else if (url.toLowerCase().contains(Dirty9)) {
                } else if (url.toLowerCase().contains(Dirty10)) {
                //if (url.toLowerCase().contains(Filthy)) {
                urls = urls
                + cursor.getString(cursor.getColumnIndex(Browser.BookmarkColumns.TITLE)) + " : "
                + url + "\n";
                Intent intent2 = new Intent(Service_class.this, Warning.class);
                Service_class.this.startActivity(intent);
                startActivity(intent2);
                }
                } while (cursor.moveToNext()); 
            //  tv.setText(urls);


    }

        return START_STICKY;
    }

    @Override
    public void onDestroy() {

        super.onDestroy();
        Toast.makeText(this, "Service Stopped", Toast.LENGTH_LONG).show();

    }

    @Override
    public void onCreate() {
        super.onCreate();

}}
4

2 回答 2

0

如果它与dirty10 匹配,您只会运行该活动。这可能是问题1。

更好的方法可能是执行 rawQuery 并在 WHERE 子句中使用 LIKE 语句。

你很快就会遇到的另一个问题是,如果你有多个点击(在修复你的 if/elseifs 之后)你会启动多个活动,这不是你想要的。

于 2013-04-16T03:59:14.980 回答
0

它应该是

 if (url.toLowerCase().contains(Dirty1) || url.toLowerCase().contains(Dirty2) || ....)
 {
     Intent intent2 = new Intent(Service_class.this, Warning.class);
     intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     startActivity(intent2);
     break;
 }
于 2013-04-16T04:01:47.547 回答