public class callLogs extends BroadcastReceiver {
String phNumber, callType, callDate, callDuration, dir;
Date callDayTime;
List<calls> data = new ArrayList<>();
SharedPreferences pref;
SharedPreferences.Editor edit;
ConnectionDetector cd;
private callsData callsData;
private callsDb callsDb;
private static final String DATABASE_NAME = "callsData";
@Override
public void onReceive(final Context context, Intent intent) {
pref = context.getSharedPreferences("pref", Context.MODE_PRIVATE);
edit = pref.edit();
callsData = Room.databaseBuilder(context.getApplicationContext(),
callsData.class, DATABASE_NAME).fallbackToDestructiveMigration().build();
// cd = new ConnectionDetector(context.getApplicationContext());
CountDownTimer countDownTimer = new CountDownTimer(500 , 1000) {
@Override
public void onTick(long l) {
}
@Override
public void onFinish() {
StringBuffer sb = new StringBuffer();
String strOrder = android.provider.CallLog.Calls.DATE + " DESC";
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_CALL_LOG) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
Cursor managedCursor = context.getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null, strOrder);
int name = managedCursor.getColumnIndex(CallLog.Calls.CACHED_NAME);
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);
sb.append("Call Details :");
managedCursor.moveToFirst();
// while (managedCursor.moveToNext()) {
String pName = managedCursor.getString(name);
phNumber = managedCursor.getString(number); // mobile number
callType = managedCursor.getString(type); // call type
callDate = managedCursor.getString(date); // call date
callDayTime = new Date(Long.valueOf(callDate));
callDuration = managedCursor.getString(duration);
dir = null;
// Log.d("Name",pName);
int dircode = Integer.parseInt(callType);
switch (dircode) {
case CallLog.Calls.OUTGOING_TYPE:
dir = "OUTGOING";
break;
case CallLog.Calls.INCOMING_TYPE:
dir = "INCOMING";
break;
case CallLog.Calls.MISSED_TYPE:
dir = "MISSED";
break;
case CallLog.Calls.REJECTED_TYPE:
dir = "REJECTED";
break;
}
new Thread(new Runnable() {
@Override
public void run() {
callsDb = new callsDb();
callsDb.setPhone(phNumber);
callsDb.setType(dir);
callsDb.setDate(String.valueOf(callDayTime));
callsDb.setDuration(callDuration);
callsData.callsDao().insertAll(callsDb);
}
}).start();
sb.append("\nPhone Number:--- " + phNumber + " \nCall Type:--- " + dir + " \nCall Date:--- " + callDayTime + " \nCall duration in sec :--- " + callDuration);
sb.append("\n----------------------------------");
// }
managedCursor.close();
// textView.setText(sb);
Log.d("Agile", sb.toString());
Log.d("data",callsDb.getPhone());
Log.d("type kya hai",callsDb.getType());
calls person = new calls();
person.setMobile(callsDb.getPhone());
person.setType(callsDb.getType());
person.setDate(callsDb.getDate());
person.setDuration(callsDb.getDuration());
data.add(person);
Bean b = (Bean) context.getApplicationContext();
Gson gson = new GsonBuilder()
.setLenient()
.create();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(b.baseURL)
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
Allapi cr = retrofit.create(Allapi.class);
callsBean body = new callsBean();
body.setCallLogs(data);
Gson gsonObj = new Gson();
String jsonStr = gsonObj.toJson(body);
String id = pref.getString("id", "");
Log.d("id", id);
Log.d("id", pref.getString("id", ""));
Log.d("id", jsonStr);
Call<callsBean> call = cr.calls(id, jsonStr);
call.enqueue(new Callback<callsBean>() {
@Override
public void onResponse(Call<callsBean> call, Response<callsBean> response) {
Log.d("response", response.body().getCallLogs().toString());
/*new Thread(new Runnable() {
@Override
public void run() {
callsData.callsDao().delete(callsDb);
}
}).start();*/
}
@Override
public void onFailure(Call<callsBean> call, Throwable t) {
}
});
}
};
countDownTimer.start();
}
我已经创建了一个
BroadcastReceiver
,因为调用代码可以正常工作并将数据存储在Room
Database
中,正在存储最新的调用,但是我正在调用Api在服务器上存储数据以及从Room
Databse
获得 但问题是Api被广播打了三下 接收器。 当呼叫进入时,第一个api将在接收时间上击中第二个api,第三个api将在结束呼叫时间上击中,这也发生在未接呼叫和呼出时间中。 请帮助我找到解决方案。 我希望Api应该只打一次