app异常,崩溃捕捉
app隐式启动
1. 最优的解决方案:
接下来我要说的这种方法可以隐藏图标,同是也可以正常启动运行。
在manifest的入口activity里面intent-filter中设置元素。
比如:
<data android:host="AuthActivity" android:scheme="com.android.example">]
或者删除
<category android:name="android.intent.category.LAUNCHER" />
或者改为
<category android:name="android.intent.category.DEFAULT" />
2. 不在历史任务列表中显示
应应用程序的AndroidManifest.xml文件中的activity标签中添加如下属性:
android:excludeFromRecents=”true”
<service
android:name=".CopyCanbusService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.example.tmedia.copycanbusservice.start" />
</intent-filter>
</service>
服务类
public class CopyCanbusService extends Service {
private static final String TAG = "CopyCanbusService";
private static CopyCanbusService instance;
private Context mContext;
public static CopyCanbusService getInstance() {
return instance;
}
@Override
public void onCreate() {
super.onCreate();
Log.d("tww", "onCreate");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("tww", "onStartCommand");
flags = START_STICKY;
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel mChannel = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
String CHANNEL_ID_STRING = "com.xyauto.dspSetting";
mChannel = new NotificationChannel(CHANNEL_ID_STRING, "xyauto", NotificationManager.IMPORTANCE_NONE);
notificationManager.createNotificationChannel(mChannel);
Notification notification = new Notification.Builder(getApplicationContext(), CHANNEL_ID_STRING).build();
startForeground(1, notification);
}
//:/system
String oldDir = Environment.getRootDirectory().toString() + "/media/Canbus";
String newDir = Environment.getExternalStorageDirectory().getAbsolutePath();
Log.d("tww", "newDir:"+newDir);
File oldFiles = new File(oldDir);//原始文件地址
String oldFileName = oldFiles.getName();//文件名字
if (!oldFiles.exists()) {
Log.d("tww", "oldFiles.exists is no");
}
File filePublic = new File(newDir, oldFileName);
if (!filePublic.exists()) {
filePublic.mkdirs();
}
copyFolder(oldFiles, filePublic);
stopForeground(true);
return super.onStartCommand(intent, flags, startId);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d("tww", "onDestroy");
}
private static void copyFolder(File newFiles, File oldFile) {
try {
File[] files = newFiles.listFiles();
for (File srcFile : files) {
//如果是文件夹
if (srcFile.isDirectory()) {
//复制过来
Log.d("tww","srcFile:"+srcFile);
String fileName = srcFile.getName();
File newFolder = new File(oldFile, fileName);
if (!newFolder.exists()) {
newFolder.mkdir();
copyFolder(srcFile, newFolder);
}
} else {
//如果是文件
String srcFileName = srcFile.getName();
File newFile = new File(oldFile, srcFileName);
Log.d("tww","newFile:"+newFile);
//字
FileInputStream fis = new FileInputStream(srcFile);
FileOutputStream fos = new FileOutputStream(newFile, false);//构建文件输出流的对象,即将文件复制在哪里去,后面的true代表每次写入时不清空当前文件内容
byte[] bytes = new byte[1024];
int ten;
while ((ten = fis.read(bytes)) != -1){
fos.write(bytes,0, ten);//1024
fos.flush();
}
fis.close();
fos.close();
Log.d("tww","close");
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.example.tmedia;
import androidx.appcompat.app.AppCompatActivity;
import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.logging.Level;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getEnvironmentDirectories();
//isGrantExternalRW(this,1);
}
public static void getEnvironmentDirectories() {
//:/system
String rootDir = Environment.getRootDirectory().toString();
String oldDir = Environment.getRootDirectory().toString() + "/media/Canbus";
String newDir = Environment.getExternalStorageDirectory().getAbsolutePath();
// String newDir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Canbus";
Log.d("tww", "sssss");
File oldFiles = new File(oldDir);//原始文件地址
String oldFileName = oldFiles.getName();//文件名字
if (!oldFiles.exists()) {
Log.d("tww", "oldFiles.mkdirs()");
return;
}
File filePublic = new File(newDir, oldFileName);
if (!filePublic.exists()) {
filePublic.mkdirs();
}
copyFolder(oldFiles, filePublic);
}
private static void copyFolder(File newFiles, File oldFile) {
try {
File[] files = newFiles.listFiles();
for (File srcFile : files) {
//如果是文件夹
if (srcFile.isDirectory()) {
//复制过来
String fileName = srcFile.getName();
File newFolder = new File(oldFile, fileName);
if (!newFolder.exists()) {
newFolder.mkdir();
copyFolder(srcFile, newFolder);
}
} else {
//如果是文件
String srcFileName = srcFile.getName();
File newFile = new File(oldFile, srcFileName);
/*
//字符流
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(srcFile)));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(newFile)));
char[] chars = new char[1024];
int len;
while ((len = br.read(chars)) != -1) {
bw.write(len);
bw.flush();
}
br.close();
bw.close();*/
//字节流
FileInputStream fis = new FileInputStream(srcFile);
FileOutputStream fos = new FileOutputStream(newFile, false);//构建文件输出流的对象,即将文件复制在哪里去,后面的true代表每次写入时不清空当前文件内容
byte[] bytes = new byte[1024];
int ten;
while ((ten = fis.read(bytes)) != -1){
fos.write(bytes,0, ten);//1024
fos.flush();
//ten = fis.read(bytes);
}
fis.close();
fos.close();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void isMkdirs() {
String storage = Environment.getExternalStorageDirectory().getPath() + "/register";
File dirFile = new File(storage);
Log.d("dirFile", "" + dirFile);
if (!dirFile.exists()) {
boolean mkdirs = dirFile.mkdirs();
if (!mkdirs) {
Log.e("TAG", "文件夹创建失败");
} else {
Log.e("TAG", "文件夹创建成功");
}
}
}
/**
* 解决安卓6.0以上版本不能读取外部存储权限的问题
*
* @param activity
* @param requestCode
* @return
*/
private static String[] PERMISSIONS_CAMERA_AND_STORAGE = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.CAMERA};
public static boolean isGrantExternalRW(Activity activity, int requestCode) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
int storagePermission = activity.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);
int cameraPermission = activity.checkSelfPermission(Manifest.permission.CAMERA);
//检测是否有权限,如果没有权限,就需要申请
if (storagePermission != PackageManager.PERMISSION_GRANTED ||
cameraPermission != PackageManager.PERMISSION_GRANTED) {
//申请权限
activity.requestPermissions(PERMISSIONS_CAMERA_AND_STORAGE, requestCode);
//返回false。说明没有授权
Log.d("tww", "false");
return false;
}
}
//说明已经授权
Log.d("tww", "true");
return true;
}
}