我正在做一个呼叫跟踪应用程序...
在我的应用程序中,有一个名为 callService 的服务,它从通话记录中获取通话并保存到数据库,然后进行检索。该代码完美运行。
该服务在切换按钮变为活动状态时启动。它是使用 AlarmManager 的 setRepeating() 函数调用的。重复时间设置为 5000,并且为了避免条目调用详细信息重复到 DB 中,使用 sharedpreference 以毫秒为单位保存上次输入的时间。通过检查最后输入的通话时间,通话详细信息进入数据库。
callService.java 如下所示。
调用服务.java:
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import android.animation.TimeInterpolator;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.IBinder;
import android.provider.CallLog;
import android.widget.Toast;
public class callService extends Service
{
DBManager db;
Cursor managedCursor;
String CallName,CallNumber,CallType,CallDate,CallTime,CallDuration,CallDATETIME, AmOrPm;
long CallTimeMilli;
Context context=this;
long timeShared;
SharedPreferences sh;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Toast.makeText(getApplicationContext(), "Call Service Created", Toast.LENGTH_SHORT).show();
db=new DBManager(this);
sh=(SharedPreferences) this.getSharedPreferences("LastCallLogEntryTime",Context.MODE_PRIVATE );
//Toast.makeText(getApplicationContext(), ""+timeShared, 3000).show();
}
@Override
@Deprecated
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
//Toast.makeText(getApplicationContext(), "Service Started", Toast.LENGTH_SHORT).show();
getCallDetails();
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Toast.makeText(getApplicationContext(), "Call Service Stopped", Toast.LENGTH_SHORT).show();
}
private void getCallDetails()
{
//StringBuffer buffer=new StringBuffer();
managedCursor=context.getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null, null);
int number=managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
int type=managedCursor.getColumnIndex(CallLog.Calls.TYPE);
int date=managedCursor.getColumnIndex(CallLog.Calls.DATE);
int duration=managedCursor.getColumnIndex(CallLog.Calls.DURATION);
int name=managedCursor.getColumnIndex(CallLog.Calls.CACHED_NAME);
//buffer.append("Call Details: ");
//int a=managedCursor.getCount();
//Toast.makeText(getApplicationContext(), "Inside GetCallDetails Function", Toast.LENGTH_SHORT).show();
while(managedCursor.moveToNext())
{
timeShared=sh.getLong("Time", 0);
String[] str={"0","0","0"};
String[] a={"0","0","0"};
String[] b={"0","0","0"};
int dd=0, mm=0, yy=0, hh=0,mn=0,sec=0;
CallName = managedCursor.getString(name);
CallNumber = managedCursor.getString( number );
String cType = managedCursor.getString( type );
//String callDate = managedCursor.getString( date );
long CallTimeMilli= managedCursor.getLong(date);
//Toast.makeText(context, ""+seconds, 3000).show();
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss a");
CallDATETIME = formatter.format(new Date(CallTimeMilli));
str=CallDATETIME.split(" ");
CallDate=str[0];
CallTime=str[1]+" "+str[2];
//AmOrPm=str[2];
/*//a=CallDate.split("-");
//Toast.makeText(getApplicationContext(), a[0]+" "+a[1]+" "+a[2], Toast.LENGTH_SHORT).show();
dd=Integer.parseInt(a[0]);
mm=Integer.parseInt(a[1]);
yy=Integer.parseInt(a[2]);
//b=CallTime.split(":");
//Toast.makeText(getApplicationContext(), b[0]+" "+b[1], Toast.LENGTH_SHORT).show();
hh=Integer.parseInt(b[0]);
mn=Integer.parseInt(b[1]);
sec=Integer.parseInt(b[2]);
//Calendar calendar=Calendar.getInstance();
//calendar.set(yy, mm, dd, hh, mn, 0);
calendar.set(calendar.YEAR, yy);
calendar.set(calendar.MONTH, mm);
calendar.set(calendar.DATE, dd);
calendar.set(calendar.HOUR, hh);
calendar.set(calendar.MINUTE, mn);
calendar.set(calendar.SECOND, sec);
calendar.set(Calendar.MILLISECOND, 0);
//Toast.makeText(getApplicationContext(), " "+calendar.getTimeInMillis(), Toast.LENGTH_SHORT).show();
//CallTimeMilli=calendar.getTimeInMillis();
//Toast.makeText(getApplicationContext(), " "+CallTimeMilli, Toast.LENGTH_SHORT).show();
//Date callDayTime = new Date(Long.valueOf(callDate));
*/
CallDuration = managedCursor.getString( duration );
CallType = null;
int dircode = Integer.parseInt( cType );
switch( dircode )
{
case CallLog.Calls.OUTGOING_TYPE:
CallType = "OUTGOING";
break;
case CallLog.Calls.INCOMING_TYPE:
CallType = "INCOMING";
break;
case CallLog.Calls.MISSED_TYPE:
CallType = "MISSED";
break;
}
//buffer.append( "\nPerson Name:-- "+CallName+"\nPhone Number:--- "+CallNumber +" \nCall Type:--- "+CallType+" \nCall Date:--- "+CallDate+" \nCall Time:--- "+CallTime+" \nCall duration in sec :--- "+CallDuration+" \nTime in Milli:--- "+CallTimeMilli );
//buffer.append("\n----------------------------------");
if(timeShared==0)
{
db.addRowCall(CallName, CallNumber, CallType, CallDate, CallTime, CallDuration, CallTimeMilli);
//Toast.makeText(getApplicationContext(), "Call Added to DB from start", Toast.LENGTH_SHORT).show();
sh.edit().putLong("Time", db.retrieveLastCallTime()).commit();
}
else
{
//timeRetrieved=db.retrieveLastCallTime();
if(CallTimeMilli>timeShared)
{
db.addRowCall(CallName, CallNumber, CallType, CallDate, CallTime, CallDuration, CallTimeMilli);
//Toast.makeText(getApplicationContext(), "......Added to DB secnd time.........", Toast.LENGTH_SHORT).show();
sh.edit().putLong("Time", db.retrieveLastCallTime()).commit();
}
}
}
managedCursor.close();
//long t=db.retrieveLastCallTime();
//Toast.makeText(getApplicationContext(), "LongValue "+t, Toast.LENGTH_SHORT).show();
}
}
使用切换按钮从 settings.java 调用该服务
call_track.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(call_track.isChecked())
{
AlarmManager al=(AlarmManager)context_settins.getSystemService(context_settins.ALARM_SERVICE);
Intent istart = new Intent(context_settins, callService.class);
PendingIntent p=PendingIntent.getService(getApplicationContext(), 123456, istart, 0);
al.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),5000, p);
startService(new Intent(context_settins, callService.class));
}
else
{
Intent intent=new Intent(context_settins, callService.class);
PendingIntent sender=PendingIntent.getService(context_settins, 123456, intent, 0);
AlarmManager alManager=(AlarmManager) context_settins.getSystemService(Context.ALARM_SERVICE);
alManager.cancel(sender);
stopService(new Intent(context_settins,callService.class));
}
}
});
问题是,当我启动服务时,它工作正常,并且数据输入到 DB,Bt,每当呼叫到来,因为时间间隔是 5000。
但是,一段时间后,应用程序强制关闭并在 log cat 中显示以下错误详细信息。
10-19 21:36:58.783: E/AndroidRuntime(298): java.lang.RuntimeException: Unable to startservice MainPRJ.Diary.callService@43e66740 with Intent { flg=0x4 cmp=MainPRJ.Diary/.callService (has extras) }: java.lang.NullPointerException
10-19 21:36:58.783: E/AndroidRuntime(298): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3063)
10-19 21:36:58.783: E/AndroidRuntime(298): at android.app.ActivityThread.access$3600(ActivityThread.java:125)
10-19 21:36:58.783: E/AndroidRuntime(298): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2096)
10-19 21:36:58.783: E/AndroidRuntime(298): at android.os.Handler.dispatchMessage(Handler.java:99)
10-19 21:36:58.783: E/AndroidRuntime(298): at android.os.Looper.loop(Looper.java:123)
10-19 21:36:58.783: E/AndroidRuntime(298): at android.app.ActivityThread.main(ActivityThread.java:4627)
10-19 21:36:58.783: E/AndroidRuntime(298): at java.lang.reflect.Method.invokeNative(Native Method)
10-19 21:36:58.783: E/AndroidRuntime(298): at java.lang.reflect.Method.invoke(Method.java:521)
10-19 21:36:58.783: E/AndroidRuntime(298): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-19 21:36:58.783: E/AndroidRuntime(298): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-19 21:36:58.783: E/AndroidRuntime(298): at dalvik.system.NativeStart.main(Native Method)
10-19 21:36:58.783: E/AndroidRuntime(298): Caused by: java.lang.NullPointerException
10-19 21:36:58.783: E/AndroidRuntime(298): at MainPRJ.Diary.callService.getCallDetails(callService.java:64)
10-19 21:36:58.783: E/AndroidRuntime(298): at MainPRJ.Diary.callService.onStart(callService.java:47)
10-19 21:36:58.783: E/AndroidRuntime(298): at android.app.Service.onStartCommand(Service.java:420)
10-19 21:36:58.783: E/AndroidRuntime(298): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3053)
10-19 21:36:58.783: E/AndroidRuntime(298): ... 10 more
我进行了很多搜索,但没有得到任何解决方案...我是 android 新手,无法理解这里发生的事情...提前致谢...