Java Android:Intent在SQLite查询匹配后不会启动
我试图在我的书签/浏览器历史记录中搜索特定的URL,然后在找到匹配项后开始新的意图。问题是——新的意图永远不会启动——尽管浏览器历史记录和书签都包含被查询的值 你知道为什么会这样吗Java Android:Intent在SQLite查询匹配后不会启动,java,android,alarmmanager,Java,Android,Alarmmanager,我试图在我的书签/浏览器历史记录中搜索特定的URL,然后在找到匹配项后开始新的意图。问题是——新的意图永远不会启动——尽管浏览器历史记录和书签都包含被查询的值 你知道为什么会这样吗 import com.parse.ParseObject; import android.app.AlertDialog; import android.app.Service; import android.content.BroadcastReceiver; import android.content.Con
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();
}}
只有当活动与dirty10匹配时,才能运行该活动。这可能是问题1 更好的方法是执行rawQuery并在WHERE子句中使用LIKE语句 你很快就会遇到的另一个问题是,如果你有多个点击(在修复if/elseifs之后),你会启动多个活动,这不是你想要的。应该是这样的
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;
}
非常感谢。我收到一个错误声明:从活动上下文外部调用startActivity()需要标志\u Activity\u NEW\u TASK标志。这真的是你想要的吗。。。尽管我已经添加了:intent2.setFlags(Intent.FLAG\u ACTIVITY\u NEW\u TASK);我没有注意,但在服务课上什么是意图。这个。开始的触觉(意图)?你真的想同时开始两项活动吗?我编辑了答案,去掉了第一个startActivity。