我绝对尝试过我能找到的每个教程以及有关stackoverflow的任何问题,我想我错过了一些东西。
我有MainActivity和3个片段CallHistory,Contacts和Sync。
我基本上要做的是请求read_call_log;当我访问呼叫历史记录部分时写入write_call_log; read_contacts;当我访问联系人部分等时写入_contacts。我需要一些指示。
LE:忘记提及,该应用程序请求权限,但它只是崩溃了该对话框,允许我在应用程序关闭后授予/拒绝权限
在 CallHistory.java 我有这个
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_call_history, container, false);
CallLogListView = (ListView) rootView.findViewById(R.id.CallLogListView);
getCallHistory();
return rootView;
}
getCallHistory() 驻留在 CallHistory.java 文件中
public void getCallHistory() {
if (checkSelfPermission(getActivity(),
Manifest.permission.READ_CALL_LOG)
!= PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.READ_CALL_LOG, Manifest.permission.WRITE_CALL_LOG}, READ_CALL_LOG_PERMISSION_VALUE);
} else {
m_CallLog = new ArrayList<CallLog>();
m_Cursor.moveToFirst();
try {
do {
CallLog item = new CallLog();
item.setCallLogType(m_Cursor.getString(m_Cursor.getColumnIndexOrThrow(android.provider.CallLog.Calls.TYPE)));
item.setCallLogNumber( m_Cursor.getString(m_Cursor.getColumnIndexOrThrow(android.provider.CallLog.Calls.NUMBER)));
item.setCallLogName(m_Cursor.getString(m_Cursor.getColumnIndexOrThrow(android.provider.CallLog.Calls.CACHED_NAME)));
item.setCallLogDuration(m_Cursor.getString(m_Cursor.getColumnIndexOrThrow(android.provider.CallLog.Calls.DURATION)));
m_CallLog.add(item);
} while(m_Cursor.moveToNext());
} finally {
if (!m_Cursor.isClosed()) {
m_Cursor.close();
}
prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
m_CallLogAdapter = new CallLogAdapter(getActivity(), R.layout.row, m_CallLog);
CallLogListView.setAdapter(m_CallLogAdapter);
CallLogListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView adapterView, View view, int position, long id) {
CharSequence colors[] = new CharSequence[] {
"Send contact info",
"Send directions",
"Save contact"
};
phoneNumber = ((TextView) view.findViewById(R.id.toptext)).getText().toString();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Action for " + phoneNumber);
builder.setItems(colors, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
AlertDialog.Builder message = new AlertDialog.Builder(getActivity());
if(which == 0)
{
Toast.makeText(getContext(), "Sending contact details to " + phoneNumber + " ...", Toast.LENGTH_SHORT).show();
sendSms(phoneNumber, prefs.getString("edit_text_preference_1", getText(R.string.default_contact_sms).toString()));
}
else if(which == 1)
{
Toast.makeText(getContext(), "Sending directions to " + phoneNumber + " ...", Toast.LENGTH_SHORT).show();
sendSms(phoneNumber, prefs.getString("edit_text_preference_2", getText(R.string.default_directions_sms).toString()));
}
else if(which == 2)
{
if(contactExists(getContext(), phoneNumber)) {
Toast.makeText(getContext(), "Contact already exists. Cheers!", Toast.LENGTH_SHORT).show();
} else {
Intent saveContactIntent = new Intent(getActivity(), ContactsAdd.class);
saveContactIntent.putExtra("phoneNumber", phoneNumber);
startActivity(saveContactIntent);
}
}
}
});
builder.show();
}
});
}
}
}
MainActivity.java
中的其他相关功能 public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
Intent intent = new Intent(this, SettingsActivity.class);
this.startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
// Handle navigation fragments
Fragment fragment = null;
Class fragmentClass = null;
if (id == R.id.nav_call_history) {
fragmentClass = CallHistory.class;
}
else if (id == R.id.nav_contacts) {
fragmentClass = Contacts.class;
}
else if (id == R.id.nav_sync) {
fragmentClass = Sync.class;
}
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
@Override
public void onFragmentInteraction(Uri uri) {
}
@Override
public void onResume()
{
super.onResume();
Fragment fragment = null;
Class fragmentClass = null;
fragmentClass = CallHistory.class;
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
List<Fragment> fragments = getSupportFragmentManager().getFragments();
if (fragments != null) {
for (Fragment fragment : fragments) {
fragment.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
}
LE - 崩溃日志
java.lang.RuntimeException: Unable to start activity ComponentInfo{uk.wrapart.crm/uk.wrapart.crm.MainActivity}: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.CallLogProvider from ProcessRecord{98fd835 18957:uk.wrapart.crm/u0a275} (pid=18957, uid=10275) requires android.permission.READ_CALL_LOG or android.permission.WRITE_CALL_LOG
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2947)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3008)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1650)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6688)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
Caused by: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.CallLogProvider from ProcessRecord{98fd835 18957:uk.wrapart.crm/u0a275} (pid=18957, uid=10275) requires android.permission.READ_CALL_LOG or android.permission.WRITE_CALL_LOG
at android.os.Parcel.readException(Parcel.java:1693)
at android.os.Parcel.readException(Parcel.java:1646)
at android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:4861)
at android.app.ActivityThread.acquireProvider(ActivityThread.java:5954)
at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2452)
at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1521)
at android.content.ContentResolver.query(ContentResolver.java:520)
at android.content.ContentResolver.query(ContentResolver.java:478)
at uk.wrapart.crm.CallHistory.onCreateView(CallHistory.java:117)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2192)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1299)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:758)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2363)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2149)
at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2013)
at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:607)
at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:178)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1256)
at android.app.Activity.performStart(Activity.java:6929)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2910)
... 9 more
LE Android Manifest
<uses-permission android:name="android.permission.READ_CALL_LOG" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
LE:CallHistory.java片段覆盖
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions,
int[] grantResults) {
if (requestCode == READ_CALL_LOG_PERMISSION_VALUE) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission is granted
getCallHistory();
} else {
Toast.makeText(getContext(), "Until you grant the permission, we canot display the names", Toast.LENGTH_SHORT).show();
}
}
}