我知道这是该板上的热门话题,但是该板上的建议和示例似乎不起作用,可能是因为它们再次更改了API28中的内容。我正在尝试共享相机拍摄的照片,但logcat显示不允许启动背景
24 23:30:48.870 1883-3164/system_process W/ActivityManager: Background start not allowed: service Intent { cmp=com.google.android.apps.photos/com.google.android.libraries.social.mediamonitor.BackgroundThreadNotifierIntentService (has extras) } to com.google.android.apps.photos/com.google.android.libraries.social.mediamonitor.BackgroundThreadNotifierIntentService from pid=7950 uid=10051 pkg=com.google.android.apps.photos startFg?=false
该项目是SDK
API 28
AndroidManifest是
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.REQUEST_COMPANION_USE_DATA_IN_BACKGROUND" />
<uses-permission android:name="android.permission.REQUEST_COMPANION_RUN_IN_BACKGROUND" />
MainActivity
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.REQUEST_COMPANION_USE_DATA_IN_BACKGROUND,
Manifest.permission.REQUEST_COMPANION_RUN_IN_BACKGROUND}, 1);
PhotoActivity
public class PhotoActivity extends AppCompatActivity
我尝试了三种在stackoverflow上找到的不同方法,但是所有这些方法在logcat中都显示相同的错误。
if (false)
{
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(Uri.fromFile(photoFile));
getApplicationContext().sendBroadcast(mediaScanIntent);
}
if (false)
{
MediaScannerConnection.scanFile(getApplicationContext(), new String[]{photoFile.getAbsolutePath()}, null, new MediaScannerConnection.OnScanCompletedListener()
{
public void onScanCompleted(String path, Uri uri)
{
int exampleCodeDidNotShowWhatToDoHere = 1;
}
});
}
MediaStore.Images.Media.insertImage(getApplicationContext().getContentResolver(),
photoFile.getAbsolutePath(), photoFile.getName(), null);
getApplicationContext().sendBroadcast(new Intent(
Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(photoFile)));