【问题标题】:about EPG data in Android TV关于 Android TV 中的 EPG 数据
【发布时间】:2017-07-18 23:25:06
【问题描述】:

我现在正在为 Android TV 创建一个 EPG 应用程序,因此我需要访问存储在“/data/data/com.android.providers.tv/databases/tv.db”中的频道数据。我试图做一个数据库助手,这是代码:

包 com.example.test;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Created by Ahmed on 18/07/2017.
 */

public class DatabaseHelper extends SQLiteOpenHelper {

    String DB_PATH=null;
    private static String DB_NAME="tvls -";
    private SQLiteDatabase myDataBase;
    private final Context myContext;


    public DatabaseHelper(Context context) {
        super(context, DB_NAME, null   , 10);
        this.myContext=context;
        this.DB_PATH="/data/data/com.android.providers.tv/databases";
        Log.e("Path 1",DB_PATH);
    }
    public void createDataBase() throws IOException{
        boolean dbExist = checkDataBase();
        Log.i("Database Existe ?",""+dbExist);
        if (dbExist){

        }else{
            this.getReadableDatabase();
            try {
                copyDataBase();
            } catch (Exception e ) {
                throw new Error("Error coying database");
            }
        }
    }

    private void copyDataBase() throws IOException {
        InputStream myInput = myContext.getAssets().open(DB_NAME);
        String outFileName = DB_PATH + DB_NAME;
        OutputStream myOutput = new FileOutputStream(outFileName);
        byte[] buffer = new byte[10];
        int length;
        while ((length=myInput.read(buffer))>0){
            myOutput.write(buffer,0,length);

        }
        myOutput.flush();
        myOutput.close();
        myInput.close();
    }

    private boolean checkDataBase() {
        SQLiteDatabase checkDB = null;
        try{
            String myPath= DB_PATH + DB_NAME;
            checkDB = SQLiteDatabase.openDatabase(myPath,null,SQLiteDatabase.OPEN_READONLY);

        }catch (SQLiteException e){
            e.printStackTrace();
        }
        if (checkDB !=null){
            checkDB.close();
        }
        return checkDB != null ? true : false;


    }
public void openDataBase() throws SQLException{
    String myPath= DB_PATH + DB_NAME;
    myDataBase = SQLiteDatabase.openDatabase(myPath,null,SQLiteDatabase.OPEN_READONLY);

}

    @Override
    public synchronized void close() {
        if(myDataBase!=null)
            myDataBase.close();
        super.close();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {}

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if (newVersion > oldVersion)
            try {
                copyDataBase();
            } catch (IOException e) {
                e.printStackTrace();

            }
    }
    public Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy) {
        return myDataBase.query("channels", null, null, null, null, null, null);
    }
}

但它没有连接到 tv.db 并抛出此错误:

07-18 23:16:01.970 5361-5361/com.example.test E/AndroidRuntime: 致命 例外:主要 进程:com.example.test,PID:5361 java.lang.Error:复制数据库时出错 在 com.example.test.DatabaseHelper.createDataBase(DatabaseHelper.java:44) 在 com.example.test.MainActivity.onCreate(MainActivity.java:46) 在 android.app.Activity.performCreate(Activity.java:6237) 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 在 android.app.ActivityThread.-wrap11(ActivityThread.java) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 在 android.os.Handler.dispatchMessage(Handler.java:102) 在 android.os.Looper.loop(Looper.java:148) 在 android.app.ActivityThread.main(ActivityThread.java:5417) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

PS:如果有任何关于如何使用 TvProvider 的信息,请帮助我卡住了。 我需要做的就是阅读频道列表和他们的节目列表。

【问题讨论】:

标签: java android android-intent android-tv


【解决方案1】:

我找到了关于这个问题的答案: 您需要通过 throw 一个 contentResolver 来获取数据并将结果查询存储在游标中。

cursor = mContext.getContentResolver().query(TvContractCompat.Channels.CONTENT_URI,
                Channel.PROJECTION,
                null,
                null,
                null);
 if (cursor != null && cursor.moveToFirst()) {
                Log.i(TAG,cursor.getCount()+"Chaines ont étaient mise à jour avec Succés");
                do {
                    channels.add(Channel.fromCursor(cursor));

                }while (cursor.moveToNext());

然后,为了获得对 epg 数据的访问权,您需要将这些行放在清单中:

   <uses-permission android:name="com.android.providers.tv.permission.READ_EPG_DATA" />

    <uses-permission android:name="com.android.providers.tv.permission.WRITE_EPG_DATA" />

    <uses-permission android:name="com.android.providers.tv.permission.ACCESS_ALL_EPG_DATA"/>

如果您将应用程序安装为用户应用程序,该应用程序将仅获取它创建的 EPG 数据,因此为了获取电视中的所有 EPG 数据,您需要将其作为系统应用程序安装在 /system/priv-应用程序文件夹。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-09
    • 1970-01-01
    • 1970-01-01
    • 2018-12-20
    • 2016-06-15
    • 1970-01-01
    相关资源
    最近更新 更多