gpt4 book ai didi

android - 怎么刷新通话记录

转载 作者:行者123 更新时间:2023-11-29 00:29:32 24 4
gpt4 key购买 nike

我正在尝试在按下按钮后刷新通话记录。我同时定义了 Uri 和 Cursor

Cursor c;
Uri allCalls = Uri.parse("content://call_log/calls");

然后在onCreate中调用这个函数

public void checkLogs() {
c = getContentResolver().query(allCalls, null, null, null, sOrder);

while (c.moveToNext()) {
num = c.getString(c.getColumnIndex(CallLog.Calls.NUMBER)); // for
// number
if (num.startsWith("+1")) {
num = num.substring(2);
}
name = c.getString(c.getColumnIndex(CallLog.Calls.CACHED_NAME)); // for
// name
type = Integer.parseInt(c.getString(c
.getColumnIndex(CallLog.Calls.TYPE))); // for type

if (type == 3 && arrlst.contains(num) == false && name == null) {
arrlst.add(num);
initialize(num);
}

else {
arrlst.add(num);
continue;
}
}

c.close();
}

这行得通,但我尝试制作一个包含以下代码的刷新按钮

    @Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case (R.id.menuRefresh):
allCalls = Uri.parse("content://call_log/calls");
checkLogs();
}
return false;
}

当我按下刷新键时,新调用不会出现,但当我关闭并打开应用程序时,它们会出现。我做错了什么?

尝试使用 Loader 时的错误日志

05-25 02:31:14.724: E/AndroidRuntime(18892): FATAL EXCEPTION: main
05-25 02:31:14.724: E/AndroidRuntime(18892): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.callfinder/com.example.callfinder.FindCalls}: java.lang.NullPointerException
05-25 02:31:14.724: E/AndroidRuntime(18892): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
05-25 02:31:14.724: E/AndroidRuntime(18892): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2356)
05-25 02:31:14.724: E/AndroidRuntime(18892): at android.app.ActivityThread.access$600(ActivityThread.java:150)
05-25 02:31:14.724: E/AndroidRuntime(18892): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
05-25 02:31:14.724: E/AndroidRuntime(18892): at android.os.Handler.dispatchMessage(Handler.java:99)
05-25 02:31:14.724: E/AndroidRuntime(18892): at android.os.Looper.loop(Looper.java:137)
05-25 02:31:14.724: E/AndroidRuntime(18892): at android.app.ActivityThread.main(ActivityThread.java:5195)
05-25 02:31:14.724: E/AndroidRuntime(18892): at java.lang.reflect.Method.invokeNative(Native Method)
05-25 02:31:14.724: E/AndroidRuntime(18892): at java.lang.reflect.Method.invoke(Method.java:511)
05-25 02:31:14.724: E/AndroidRuntime(18892): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
05-25 02:31:14.724: E/AndroidRuntime(18892): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
05-25 02:31:14.724: E/AndroidRuntime(18892): at dalvik.system.NativeStart.main(Native Method)
05-25 02:31:14.724: E/AndroidRuntime(18892): Caused by: java.lang.NullPointerException
05-25 02:31:14.724: E/AndroidRuntime(18892): at com.example.callfinder.FindCalls.checkLogs(FindCalls.java:171)
05-25 02:31:14.724: E/AndroidRuntime(18892): at com.example.callfinder.FindCalls.onCreate(FindCalls.java:55)
05-25 02:31:14.724: E/AndroidRuntime(18892): at android.app.Activity.performCreate(Activity.java:5104)
05-25 02:31:14.724: E/AndroidRuntime(18892): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
05-25 02:31:14.724: E/AndroidRuntime(18892): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2260)
05-25 02:31:14.724: E/AndroidRuntime(18892): ... 11 more

还有我试图解释的代码,如果它在评论中令人困惑

public void initialize() {
layout = (LinearLayout) findViewById(R.id.ll1);
tvTitle = (TextView) findViewById(R.id.textView1);
lp = new LayoutParams(LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
sOrder = String.format(sDate + " %s", sSort);

mAdapter = new SimpleCursorAdapter(this, R.layout.activity_find_calls,
null, null, null, 0);
getLoaderManager().initLoader(0, null, this);

}

@Override
public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
return new CursorLoader(this, allCalls,
null, null, null, sOrder);
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
switch (loader.getId()) {
case 1:
mAdapter.swapCursor(cursor);
break;
}

}

@Override
public void onLoaderReset(Loader<Cursor> arg0) {
mAdapter.swapCursor(null);

}

    public void checkLogs() {
c = mAdapter.getCursor();
//c = getContentResolver().query(allCalls, null, null, null, sOrder);

while (c.moveToNext()) {
num = c.getString(c.getColumnIndex(CallLog.Calls.NUMBER)); // for
// number
if (num.startsWith("+1")) {
num = num.substring(2);
}
name = c.getString(c.getColumnIndex(CallLog.Calls.CACHED_NAME)); // for
// name
type = Integer.parseInt(c.getString(c
.getColumnIndex(CallLog.Calls.TYPE))); // for type

if (type == 3 && arrlst.contains(num) == false && name == null) {
arrlst.add(num);
makeButton(num);
}

else {
arrlst.add(num);
continue;
}
}


}

最佳答案

使用CursorLoader 查询包含调用日志记录的游标。 CursorLoader 会在修改调用日志时自动刷新,因此您不必担心手动刷新显示(如果确实需要手动刷新,您可以调用 LoaderManager restartLoader() 方法)。

关于android - 怎么刷新通话记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16727487/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com