首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >关于Android电视中的EPG数据

关于Android电视中的EPG数据
EN

Stack Overflow用户
提问于 2017-07-18 23:25:06
回答 1查看 1.6K关注 0票数 1

我现在正在为android创建一个EPG应用程序,所以我需要访问存储在"/data/data/com.android.providers.tv/databases/tv.db".中的频道数据我试着做了一个数据库助手--这是代码:

包装com.example.test;

代码语言:javascript
运行
复制
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:错误隐藏数据库在java.lang.Error在com.example.test.MainActivity.onCreate(MainActivity.java:46) at android.app.Activity.performCreate(Activity.java:6237) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)在android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)在android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148)在android.app.ActivityThread.main(ActivityThread.java:5417)在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)的java.lang.reflect.Method.invoke(原生方法)在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-25 17:32:52

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

代码语言:javascript
运行
复制
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数据的访问,您需要将这些行放在清单中:

代码语言:javascript
运行
复制
   <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-app文件夹中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45178632

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档