我正在尝试通过广播将电话和短信登录到列表视图中。 问题是当应用程序运行时,会出现一个空列表视图,其中包含每个元素的水平线。当您单击任何元素时,其中的数据会闪烁,当您释放单击时,listview将再次变为空白。
请帮助我哪里出错?
LogServiceActivity.java
package com.logservice;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.ListActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Menu;
import android.widget.ArrayAdapter;
public class LogServiceActivity extends ListActivity {
public MyResultReceiver mReceiver;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_log_service);
//start the service
mReceiver = new MyResultReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ServiceClass.MY_ACTION);
registerReceiver(mReceiver, intentFilter);
startService(new Intent(getApplicationContext(), ServiceClass.class));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_log_service, menu);
return true;
}
@Override
protected void onStop() {
unregisterReceiver(mReceiver);
super.onStop();
}
public class MyResultReceiver extends BroadcastReceiver {
ArrayList<String> callList = new ArrayList<String>();
@Override
public void onReceive(Context context, Intent intent) {
String log = intent.getStringExtra("DATAPASSED");
callList.add(log);
setListAdapter(new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, callList));
}
}
}
ServiceClass.java:
package com.logservice;
import java.text.DateFormat;
import android.app.Service;
import android.content.Intent;
import android.database.Cursor;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class ServiceClass extends Service {
Intent intent = new Intent();
private static final String TAG = null;
final static String MY_ACTION = "My ACTION";
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
public void onCreate() {
super.onCreate();
Cursor cursor =getContentResolver().query(
android.provider.CallLog.Calls.CONTENT_URI, null, null, null,
android.provider.CallLog.Calls.DATE + " DESC ");
int numberColumnId = cursor.getColumnIndex(android.provider.CallLog.Calls.NUMBER);
int durationId = cursor.getColumnIndex(android.provider.CallLog.Calls.DURATION);
int contactNameId = cursor.getColumnIndex(android.provider.CallLog.Calls.CACHED_NAME);
int dateId = cursor.getColumnIndex(android.provider.CallLog.Calls.DATE);
int numTypeId = cursor.getColumnIndex(android.provider.CallLog.Calls.CACHED_NUMBER_TYPE);
// ArrayList<String> callList = new ArrayList<String>();
String result;
if (cursor.moveToFirst()) {
do{
String contactNumber = cursor.getString(numberColumnId);
String contactName = cursor.getString(contactNameId);
String duration = cursor.getString(durationId);
String callDate = DateFormat.getDateInstance().format(dateId);
result ="Contact Number: " + contactNumber
+ "\nContact Name: " + contactName + "\nDuration: "
+ duration + "\nDate: " + callDate;
intent.setAction(MY_ACTION);
intent.putExtra("DATAPASSED",result);
sendBroadcast(intent);
} while(cursor.moveToNext());
}
}
@Override
public void onStart(Intent intent, int startId) {
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
}
@Override
public void onDestroy() {
Toast.makeText(this, "MyService Stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
}
}
布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LogServiceActivity" >
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</LinearLayout>
请帮助。谢谢!