一个简单的应用程序,用于搜索UserDictionary.Words.CONTENT_URI
表中用户输入的单词。我正在使用BlueStacks模拟器。
错误日志表明我的android.permission.READ_USER_DICTIONARY
中没有声明android.permission.WRITE_USER_DICTIONARY
或AndroidManifest
。但我已经宣布:
<uses-permission android:name="ANDROID.PERMISSION.READ_USER_DICTIONARY"/>
<uses-permission android:name="ANDROID.PERMISSION.WRITE_USER_DICTIONARY"/>
如何解决这个问题?
错误日志:
01-26 21:14:03.361 5988-6040/system_process E/InputDispatcher﹕ Motion event has invalid pointer count 0; value must be between 1 and 16.
01-26 21:14:03.461 5988-6040/system_process E/InputDispatcher﹕ Motion event has invalid pointer count 0; value must be between 1 and 16.
01-26 21:14:03.461 9025-9025/com.example.contentresolver2 E/AndroidRuntime﹕ in writeCrashedAppName, pkgName :com.example.contentresolver2
01-26 21:14:03.471 9025-9025/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.contentresolver2, PID: 9025
java.lang.SecurityException: Permission Denial: opening provider com.android.providers.userdictionary.UserDictionaryProvider from ProcessRecord{4b18dc58 9025:com.example.contentresolver2/u0a67} (pid=9025, uid=10067) requires android.permission.READ_USER_DICTIONARY or android.permission.WRITE_USER_DICTIONARY
at android.os.Parcel.readException(Parcel.java:1465)
at android.os.Parcel.readException(Parcel.java:1419)
at android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:2848)
at android.app.ActivityThread.acquireProvider(ActivityThread.java:4419)
at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2213)
at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1425)
at android.content.ContentResolver.query(ContentResolver.java:445)
at android.content.ContentResolver.query(ContentResolver.java:404)
at com.example.contentresolver2.MainActivity.performSearch(MainActivity.java:64)
at com.example.contentresolver2.MainActivity.onClick(MainActivity.java:29)
at android.view.View.performClick(View.java:4443)
at android.view.View$PerformClick.run(View.java:18433)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5021)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
at dalvik.system.NativeStart.main(Native Method)
AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.contentresolver2"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="19"/>
<uses-permission android:name="ANDROID.PERMISSION.READ_USER_DICTIONARY"/>
<uses-permission android:name="ANDROID.PERMISSION.WRITE_USER_DICTIONARY"/>
<application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
<activity android:name="MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java:
package com.example.contentresolver2;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.UserDictionary;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements View.OnClickListener {
private Cursor mCursor;
private static final String LOG_TAG = "LOG_TAG";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
(findViewById(R.id.btnSearch)).setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnSearch:
performSearch();
break;
}
}
/**
* Read the word from the input and search in Android UserDictionary.Words table
*/
private void performSearch() {
// Select columns for projection
String[] projection = {
UserDictionary.Words._ID,
UserDictionary.Words.WORD,
UserDictionary.Words.LOCALE
};
String selectionClause = null;
String[] selectionArgs = {""}; // only one element array
String sortOrder = null;
// Read user input
String userInput = ((EditText) (findViewById(R.id.etSearchString))).getText().toString();
// Decide what kind of request to perform
if (TextUtils.isEmpty(userInput)) {
// Setting the selection clause to null will return all words
selectionClause = null;
selectionArgs[0] = "";
} else {
// Or setting the selection clause to find the word from the input
selectionClause = UserDictionary.Words.WORD + "= ?";
selectionArgs[0] = userInput;
}
// Perform query
mCursor = getContentResolver().query(
UserDictionary.Words.CONTENT_URI,
projection,
selectionClause,
selectionArgs,
sortOrder
);
// Some providers return null if an error occurs, others throw an exception
if (null == mCursor) {
Log.e(LOG_TAG, "MainActivity > performSearch(): mCursor is null");
// If the Cursor is empty, the provider found no matches
} else if (mCursor.getCount() < 1) {
Log.e(LOG_TAG, "MainActivity > performSearch(): mCursor mCursor is empty");
// Results:
} else {
mCursor.moveToFirst();
String resultString = mCursor.getString(mCursor.getColumnIndex(UserDictionary.Words.WORD));
((TextView) (findViewById(R.id.tvResultString))).setText(resultString);
}
}
}
main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:textSize="20sp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Search in UserDictionary"
/>
<EditText
android:hint="Input a word to search in UserDictionary"
android:id="@+id/etSearchString"
android:layout_width="350dp"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btnSearch"
android:text="Search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tvResultString"
android:text="___"
android:textSize="20sp"
android:textColor="#FFFF00"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
/>
</LinearLayout>