API 21 中的 getOrCreateThreadId
getOrCreateThreadId in API 21
我目前正在开发一个相当基本的消息传递应用程序,目标是 API 21 级。我不能再高了。在版本 21 (Android 5.0).
中实现替代 Threads.getOrCreateThreadId
方法(需要 API 23)的最简单方法是什么
另外我的理解是该方法的第二个参数将接受一个 phone 数字,我这样想对吗?
这样做的目的是在 JavaScriptInterface 方法中使用,因为我使用 HTML 作为我的布局,因为它更容易使用。我也不想使用 Android SDK 中不可见的 API。
谷歌搜索后,我发现 this compatibility class 具有我需要的功能。我复制了其中两个,根据我的需要稍微调整它们。他们在这里:
private static long getOrCreateThreadIdInternal(Context context, Set<String> recipients) {
Uri.Builder uriBuilder = Uri.parse("content://mms-sms/threadID").buildUpon();
for (String recipient : recipients) {
/*if (isEmailAddress(recipient)) {
recipient = extractAddrSpec(recipient);
}*/
uriBuilder.appendQueryParameter("recipient", recipient);
}
Uri uri = uriBuilder.build();
Cursor cursor = query(context.getContentResolver(), uri, new String[] {BaseColumns._ID}, null, null, null);
if (cursor != null) {
try {
if (cursor.moveToFirst()) {
return cursor.getLong(0);
} else {
Log.e("JavaScriptInterface", "getOrCreateThreadId returned no rows!");
}
} finally {
cursor.close();
}
}
Log.e("JavaScriptInterface", "getOrCreateThreadId failed with uri " + uri.toString());
throw new IllegalArgumentException("Unable to find or allocate a thread ID.");
}
private static Cursor query(ContentResolver resolver, Uri uri, String[] projection,
String selection, String[] selectionArgs, String sortOrder) {
try {
return resolver.query(uri, projection, selection, selectionArgs, sortOrder);
} catch (Exception e) {
Log.e("JavaScriptInterface", "Catch an exception when query: ", e);
return null;
}
}
此代码不归功于我。
我目前正在开发一个相当基本的消息传递应用程序,目标是 API 21 级。我不能再高了。在版本 21 (Android 5.0).
中实现替代Threads.getOrCreateThreadId
方法(需要 API 23)的最简单方法是什么
另外我的理解是该方法的第二个参数将接受一个 phone 数字,我这样想对吗?
这样做的目的是在 JavaScriptInterface 方法中使用,因为我使用 HTML 作为我的布局,因为它更容易使用。我也不想使用 Android SDK 中不可见的 API。
谷歌搜索后,我发现 this compatibility class 具有我需要的功能。我复制了其中两个,根据我的需要稍微调整它们。他们在这里:
private static long getOrCreateThreadIdInternal(Context context, Set<String> recipients) {
Uri.Builder uriBuilder = Uri.parse("content://mms-sms/threadID").buildUpon();
for (String recipient : recipients) {
/*if (isEmailAddress(recipient)) {
recipient = extractAddrSpec(recipient);
}*/
uriBuilder.appendQueryParameter("recipient", recipient);
}
Uri uri = uriBuilder.build();
Cursor cursor = query(context.getContentResolver(), uri, new String[] {BaseColumns._ID}, null, null, null);
if (cursor != null) {
try {
if (cursor.moveToFirst()) {
return cursor.getLong(0);
} else {
Log.e("JavaScriptInterface", "getOrCreateThreadId returned no rows!");
}
} finally {
cursor.close();
}
}
Log.e("JavaScriptInterface", "getOrCreateThreadId failed with uri " + uri.toString());
throw new IllegalArgumentException("Unable to find or allocate a thread ID.");
}
private static Cursor query(ContentResolver resolver, Uri uri, String[] projection,
String selection, String[] selectionArgs, String sortOrder) {
try {
return resolver.query(uri, projection, selection, selectionArgs, sortOrder);
} catch (Exception e) {
Log.e("JavaScriptInterface", "Catch an exception when query: ", e);
return null;
}
}
此代码不归功于我。