Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 广播接收机中的数据库打开困难_Android_Sqlite_Broadcastreceiver - Fatal编程技术网

Android 广播接收机中的数据库打开困难

Android 广播接收机中的数据库打开困难,android,sqlite,broadcastreceiver,Android,Sqlite,Broadcastreceiver,我在班上打开数据库有困难。我试图让我的应用程序在手机启动时启动,这只是在数据库中的布尔值为true时,如果为false,则不会在启动时启动应用程序。我把错误都记在下面了 public class My_BroadcastReceiver extends BroadcastReceiver { SQLiteDatabase sqlDB; private static String DBNAME2 = "database2.db"; private static String Table2

我在班上打开数据库有困难。我试图让我的应用程序在手机启动时启动,这只是在数据库中的布尔值为true时,如果为false,则不会在启动时启动应用程序。我把错误都记在下面了

public class My_BroadcastReceiver extends BroadcastReceiver {   

SQLiteDatabase sqlDB;
private static String DBNAME2 = "database2.db";
private static String Table2 = "options";

Boolean isOk = true;
Context context;

@Override  
public void onReceive(Context context, Intent intent) {


    try {

        sqlDB = SQLiteDatabase.openOrCreateDatabase(DBNAME2, null);

        Cursor r = sqlDB.rawQuery("SELECT Apponstart FROM " + Table2, null);
        System.out.println("COUNT of Location: " + r.getCount());
        int loc = r.getColumnIndex("Apponstart");

        int j = 0;
        if (r.moveToFirst()) {
            do {
                System.out.println("Apponstart is " + r.getString(loc));

                isOk = Boolean.parseBoolean(r.getString(loc));

                j++;
            } while (r.moveToNext());
            if (j == 0) {
                System.out.println("No data found");
            }

        }
        r.close();
        // sqlDB.close();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        sqlDB.close();
    }

    if(isOk){
        Intent i = new Intent(context, JoshTwoActivity.class);                   
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);                
        context.startActivity(i); 
    }
}



 09-23 08:10:05.615: WARN/System.err(208): android.database.sqlite.SQLiteException: unable to open database file
        09-23 08:10:05.625: WARN/System.err(208):     at android.database.sqlite.SQLiteDatabase.dbopen(Native Method)
        09-23 08:10:05.625: WARN/System.err(208):     at                android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1812)
        09-23 08:10:05.625: WARN/System.err(208):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:817)
        09-23 08:10:05.625: WARN/System.err(208):     at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:851)
        09-23 08:10:05.625: WARN/System.err(208):     at three.three.My_BroadcastReceiver.onReceive(My_BroadcastReceiver.java:31)
         09-23 08:10:05.625: WARN/System.err(208):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2810)
         09-23 08:10:05.625: WARN/System.err(208):     at android.app.ActivityThread.access$3200(ActivityThread.java:125)
           09-23 08:10:05.625: WARN/System.err(208):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2083)
          09-23 08:10:05.625: WARN/System.err(208):     at android.os.Handler.dispatchMessage(Handler.java:99)
            09-23 08:10:05.625: WARN/System.err(208):     at android.os.Looper.loop(Looper.java:123)
          09-23 08:10:05.625: WARN/System.err(208):     at android.app.ActivityThread.main(ActivityThread.java:4627)
          09-23 08:10:05.625: WARN/System.err(208):     at java.lang.reflect.Method.invokeNative(Native Method)
          09-23 08:10:05.625: WARN/System.err(208):     at java.lang.reflect.Method.invoke(Method.java:521)
         09-23 08:10:05.625: WARN/System.err(208):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
         09-23 08:10:05.625: WARN/System.err(208):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
         09-23 08:10:05.625: WARN/System.err(208):     at dalvik.system.NativeStart.main(Native Method)

可能需要更多的代码来解决这个问题,但我想问题与接收器的注册方式有关。您是用XML还是通过registerReceiver进行声明?有人联系过这个onReceive吗


阅读此参考页上的BroadcastReceiver生命周期:

数据库是问题所在。我创建了一个方法,并将上下文添加到数据库的打开中。这是我的解决办法

public void guru(Context ctx){

    System.out.println("Flax");
    try {
        Log.e("troll", "alol");
        sqlDB = ctx.openOrCreateDatabase(DBNAME2, 0,null); 
        //sqlDB = SQLiteDatabase.openOrCreateDatabase(DBNAME2, null);           
        Cursor r = sqlDB.rawQuery("SELECT Apponstart FROM " + Table2, null);
        System.out.println("COUNT of Location: " + r.getCount());
        int loc = r.getColumnIndex("Apponstart");

        int j = 0;
        if (r.moveToFirst()) {
            do {
                System.out.println("Apponstart is " + r.getString(loc));
                Log.d("happening", "here");
                isOk = Boolean.parseBoolean(r.getString(loc));

                j++;
            } while (r.moveToNext());
            if (j == 0) {
                System.out.println("No data found");
            }

        }
        r.close();
        // sqlDB.close();
    } catch (Exception e) {
        e.printStackTrace();
        Log.e("exception", e.toString());
    } finally {
        sqlDB.close();
    }
}

为什么不使用日志记录和/或调试器来查找问题?您在
adb logcat
中获得任何日志消息(即使是来自引导时的日志消息),那么您的问题在哪里?顺便说一句:
System.out
不是登录Android的理想方式。我不知道这一点。谢谢。我在Manifest.xml中注册了接收者。当我取出数据库代码时,OnReceive起作用。如果我不使用简单的StartTactivity,它可以正常工作
public void guru(Context ctx){

    System.out.println("Flax");
    try {
        Log.e("troll", "alol");
        sqlDB = ctx.openOrCreateDatabase(DBNAME2, 0,null); 
        //sqlDB = SQLiteDatabase.openOrCreateDatabase(DBNAME2, null);           
        Cursor r = sqlDB.rawQuery("SELECT Apponstart FROM " + Table2, null);
        System.out.println("COUNT of Location: " + r.getCount());
        int loc = r.getColumnIndex("Apponstart");

        int j = 0;
        if (r.moveToFirst()) {
            do {
                System.out.println("Apponstart is " + r.getString(loc));
                Log.d("happening", "here");
                isOk = Boolean.parseBoolean(r.getString(loc));

                j++;
            } while (r.moveToNext());
            if (j == 0) {
                System.out.println("No data found");
            }

        }
        r.close();
        // sqlDB.close();
    } catch (Exception e) {
        e.printStackTrace();
        Log.e("exception", e.toString());
    } finally {
        sqlDB.close();
    }
}