gpt4 book ai didi

java - 关于 Android TV 中的 EPG 数据

转载 作者:太空宇宙 更新时间:2023-11-04 11:18:39 29 4
gpt4 key购买 nike

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

包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: FATAL EXCEPTION: main Process: com.example.test, PID: 5361 java.lang.Error: Error coying database at com.example.test.DatabaseHelper.createDataBase(DatabaseHelper.java:44) at 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) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

PS:如果有任何有关如何使用 TvProvider 的信息,请帮助我,我被困住了。我需要做的就是阅读 channel 列表及其节目列表。

最佳答案

我找到了这个问题的答案: 您需要通过抛出一个 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 数据,您需要将这些行放入 list 中:

   <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 文件夹中。

关于java - 关于 Android TV 中的 EPG 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45178632/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com