关于 Android 电视中的 EPG 数据
about EPG data in Android TV
我现在正在为 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: 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 的信息,请帮助我。
我需要做的就是阅读频道列表及其节目列表。
我找到了关于这个问题的答案:
你需要通过抛出一个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-app文件夹。
我现在正在为 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: 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 的信息,请帮助我。 我需要做的就是阅读频道列表及其节目列表。
我找到了关于这个问题的答案: 你需要通过抛出一个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-app文件夹。