我需要在安装时从android平台的离子(3.9.2)项目中的文本文件中创建所有logcat行的文件。
在原生的android项目中,我们在Application类中写东西,但对于离子没有任何想法。
还为应用程序创建了自定义插件,是否可以从插件执行安装方法?
答案 0 :(得分:1)
终于得到了解决方案。
从自定义插件中添加了自定义应用程序类,用于生成日志文件。
对于清单条目,修改plugin.xml以使所有工作正常工作。
我创建了应用程序类bellow,
public class CustomApplication extends Application{
private static final String TAG = "CustomApplication";
/**
* Called when the application is starting, before any activity, service, or receiver objects (excluding content providers) have been created.
*/
public void onCreate()
{
Log.e(TAG, "from onCreate");
super.onCreate();
if (isExternalStorageWritable())
{
Log.e(TAG,"file path : " + this.getExternalFilesDir(null).getPath() + "/MyApp");
File appDirectory = new File(this.getExternalFilesDir(null).getPath() + "/MyApp");
File logDirectory = new File(appDirectory + "/log");
File logFile = new File(logDirectory, "logcat" + ".txt");
// create app folder
if (!appDirectory.exists())
{
appDirectory.mkdir();
}
// create log folder
if (!logDirectory.exists())
{
logDirectory.mkdir();
}
// clear the previous logcat and then write the new one to the file
try
{
Process process = Runtime.getRuntime().exec("logcat -c");
process = Runtime.getRuntime().exec("logcat -f " + logFile);
} catch (IOException e)
{
e.printStackTrace();
}
} else if (isExternalStorageReadable())
{
Log.e(TAG,"only readable external storage");
// only readable
} else
{
Log.e(TAG,"not accessible external storage");
// not accessible
}
}
/* Checks if external storage is available for read and write */
public boolean isExternalStorageWritable()
{
String state = Environment.getExternalStorageState();
if ( Environment.MEDIA_MOUNTED.equals( state ) ) {
return true;
}
return false;
}
/* Checks if external storage is available to at least read */
public boolean isExternalStorageReadable()
{
String state = Environment.getExternalStorageState();
if ( Environment.MEDIA_MOUNTED.equals( state ) ||
Environment.MEDIA_MOUNTED_READ_ONLY.equals( state ) ) {
return true;
}
return false;
}
}
Cordova插件对Application的plugin.xml文件修改如下,
<source-file src="src/android/CustomApplication.java" target-dir="src/com/MyApplication" />
<edit-config file="AndroidManifest.xml" target="/manifest/application" mode="merge">
<application android:name="CustomApplication"/>
</edit-config>