大家好,我正在尝试制作铃声应用程序,几天前曾问过一个考虑权限的问题,您的回答对我帮助很大。原始问题:尝试在android studio中将歌曲设置为特定朋友时应用程序崩溃,getContentresolver应用程序崩溃 我设法通过使用此功能为特定朋友设置铃声:
public void setFriend(Intent data)
{
if(data != null) {
Uri contactData = data.getData();
String contactId = contactData.getLastPathSegment();
String[] PROJECTION = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.HAS_PHONE_NUMBER,
};
Cursor localCursor = getContentResolver().query(contactData, PROJECTION, null, null, null);
localCursor.moveToFirst();
//--> use moveToFirst instead of this: localCursor.move(Integer.valueOf(contactId)); /*CONTACT ID NUMBER*/
String contactID = localCursor.getString(localCursor.getColumnIndexOrThrow("_id"));
String contactDisplayName = localCursor.getString(localCursor.getColumnIndexOrThrow("display_name"));
Uri localUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, contactID);
localCursor.close();
ContentValues localContentValues = new ContentValues();
Uri uri = Uri.parse("android.resource://" + getBaseContext().getPackageName() + "/raw/" + nizModela.get(customAdapter.lastSetAsPosition).getNaziv());
File f = customAdapter.SaveFileOnInternalMemory(uri, customAdapter.lastSetAsPosition, getContentResolver());
localContentValues.put(ContactsContract.Data.RAW_CONTACT_ID, contactId);
localContentValues.put(ContactsContract.Data.CUSTOM_RINGTONE, f.getAbsolutePath());
getContentResolver().update(localUri, localContentValues, null, null);
Toast.makeText(this, "Ringtone assigned to: " + contactDisplayName, Toast.LENGTH_LONG).show();
}
}
关于活动结果:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == PICK_CONTACT)
{
//ako je api nizi od 23 netreba permitioni
this.data = data;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M )
{
int PERMISSION_ALL = 1;
String[] PERMISSIONS = {
//Manifest.permission.READ_CONTACTS,
Manifest.permission.WRITE_CONTACTS,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
};
// Manifest.permission.READ_SMS, Manifest.permission.CAMERA
if(!hasPermissions(this, PERMISSIONS)){
ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
}
else{
setFriend(data);
}
}
else
setFriend(data);
}
if(callbackManager.onActivityResult(requestCode, resultCode, data)) {
return;
}
}
并根据请求许可结果:
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
final int REQUEST_ID_MULTIPLE_PERMISSIONS = 1;
switch (requestCode) {
case REQUEST_ID_MULTIPLE_PERMISSIONS: {
Map<String, Integer> perms = new HashMap<>();
// Initialize the map with both permissions
perms.put(Manifest.permission.WRITE_CONTACTS, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.WRITE_EXTERNAL_STORAGE, PackageManager.PERMISSION_GRANTED);
// Fill with actual results from user
if (grantResults.length > 0) {
for (int i = 0; i < permissions.length; i++)
perms.put(permissions[i], grantResults[i]);
// Check for both permissions
if (perms.get(Manifest.permission.WRITE_CONTACTS) == PackageManager.PERMISSION_GRANTED
&& perms.get(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this,"Permisije dozvoljene ",Toast.LENGTH_SHORT).show();
setFriend(data);
// Log.d(TAG, "sms & location services permission granted");
// process the normal flow
//else any one or both the permissions are not granted
} else {
Toast.makeText(this,"Permisije nisu dozvolje",Toast.LENGTH_SHORT).show();
//permission is denied (this is the first time, when "never ask again" is not checked) so ask again explaining the usage of permission
// // shouldShowRequestPermissionRationale will return true
//show the dialog or snackbar saying its necessary and try again otherwise proceed with setup.
}
}
break;
}
}
所以问题是当我通过调试器运行它时,一切都运行顺利。但是,当我为特定朋友设置铃声并尝试对其进行测试时,当该特定联系人 在我的手机上呼叫我时,我的手机重新启动并且我设法在设备监视器中捕捉到此错误,这与DISPLAY有关。老实说我不知道怎么办,求救!!:
/DisplayManager: Could not get display information from display manager.
android.os.DeadObjectException
at android.os.BinderProxy.transactNative(Native Method)
at android.os.BinderProxy.transact(Binder.java:503)
at android.hardware.display.IDisplayManager$Stub$Proxy.getDisplayInfo(IDisplayManager.java:265)
at android.hardware.display.DisplayManagerGlobal.getDisplayInfo(DisplayManagerGlobal.java:119)
at android.view.Display.updateDisplayInfoLocked(Display.java:864)
at android.view.Display.getMetrics(Display.java:779)
at com.facebook.acra.CrashTimeDataCollector.toString(:21499)
at com.facebook.acra.CrashTimeDataCollector.getConstantDeviceData(:21230)
at com.facebook.acra.CrashTimeDataCollector.populateConstantDeviceData(:21314)
at com.facebook.acra.CrashTimeDataCollector.gatherCrashData(:21179)
at com.facebook.acra.ErrorReporter.handleExceptionInternal(:4307)
at com.facebook.acra.ErrorReporter.uncaughtExceptionImpl(:4914)
at com.facebook.acra.ErrorReporter.uncaughtException(:4878)
at X.0LG.uncaughtException(:49365)
at X.04b.uncaughtException(:14078)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)
09-09 14:07:29.976 17670-17850/? E/DisplayManager: Could not get display information from display manager.
android.os.DeadObjectException
at android.os.BinderProxy.transactNative(Native Method)
at android.os.BinderProxy.transact(Binder.java:503)
at android.hardware.display.IDisplayManager$Stub$Proxy.getDisplayInfo(IDisplayManager.java:265)
at android.hardware.display.DisplayManagerGlobal.getDisplayInfo(DisplayManagerGlobal.java:119)
at android.view.Display.updateDisplayInfoLocked(Display.java:864)
at android.view.Display.updateCachedAppSizeIfNeededLocked(Display.java:888)
at android.view.Display.getWidth(Display.java:562)
at com.facebook.acra.CrashTimeDataCollector.toString(:21501)
at com.facebook.acra.CrashTimeDataCollector.getConstantDeviceData(:21230)
at com.facebook.acra.CrashTimeDataCollector.populateConstantDeviceData(:21314)
at com.facebook.acra.CrashTimeDataCollector.gatherCrashData(:21179)
at com.facebook.acra.ErrorReporter.handleExceptionInternal(:4307)
at com.facebook.acra.ErrorReporter.uncaughtExceptionImpl(:4914)
at com.facebook.acra.ErrorReporter.uncaughtException(:4878)
at X.0LG.uncaughtException(:49365)
at X.04b.uncaughtException(:14078)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)
09-09 14:07:29.977 17670-17850/? E/DisplayManager: Could not get display information from display manager.
android.os.DeadObjectException
at android.os.BinderProxy.transactNative(Native Method)
at android.os.BinderProxy.transact(Binder.java:503)
at android.hardware.display.IDisplayManager$Stub$Proxy.getDisplayInfo(IDisplayManager.java:265)
at android.hardware.display.DisplayManagerGlobal.getDisplayInfo(DisplayManagerGlobal.java:119)
at android.view.Display.updateDisplayInfoLocked(Display.java:864)
at android.view.Display.getRefreshRate(Display.java:644)
at com.facebook.acra.CrashTimeDataCollector.toString(:21503)
at com.facebook.acra.CrashTimeDataCollector.getConstantDeviceData(:21230)
at com.facebook.acra.CrashTimeDataCollector.populateConstantDeviceData(:21314)
at com.facebook.acra.CrashTimeDataCollector.gatherCrashData(:21179)
at com.facebook.acra.ErrorReporter.handleExceptionInternal(:4307)
at com.facebook.acra.ErrorReporter.uncaughtExceptionImpl(:4914)
at com.facebook.acra.ErrorReporter.uncaughtException(:4878)
at X.0LG.uncaughtException(:49365)
at X.04b.uncaughtException(:14078)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)