FAQS


I have an android project in school. It is an app that can view both SMS and CALL log. my problem is, my prof wants me to have an option of viewing this in current day, weekly or monthly, but i dont know how to do it.!

Views: 1121  |   Date: 2014-02-23 16:18:23

this is my current code...

package edu.chenny.log_meter;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;

import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.CallLog;
import android.provider.CallLog.Calls;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContextWrapper;
import android.database.Cursor;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class LogMeterActivity extends Activity {
public static final ContextWrapper currentContext = null;

LayoutInflater inflater;

ScrollView svContainer;
LinearLayout llContainer;
View llLoading;
TextView tvSMSReceived, tvSMSSent, tvSMSCost, tvCallsIncoming, tvCallsOutgoing, tvCallsCost;

Button reset, exit;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_log_meter);

inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
llLoading = inflater.inflate(R.layout.loading, null, false);

svContainer = (ScrollView)findViewById(R.id.svContainer);
llContainer = (LinearLayout)findViewById(R.id.llContainer);

tvSMSReceived = (TextView)findViewById(R.id.vTotalSMSReceived);
tvSMSSent = (TextView)findViewById(R.id.vTotalSMSSent);
tvSMSCost = (TextView)findViewById(R.id.vTotalSMSCost);

tvCallsIncoming = (TextView)findViewById(R.id.vTotalCallIncoming);
tvCallsOutgoing = (TextView)findViewById(R.id.vTotalCallOutgoing);
tvCallsCost = (TextView)findViewById(R.id.vTotalCallCost);

reset = (Button) findViewById (R.id.reset);
exit = (Button) findViewById (R.id.exit);

svContainer.removeAllViews();
svContainer.addView(llLoading);


new LogLoader().execute();

/*reset.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Reset();
}
});*/

exit.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish();
}
});

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_log_meter, menu);
return true;
}

class LogLoader extends AsyncTask {

@Override
protected HashMap doInBackground(Void... params) {
HashMap map = new HashMap();
ContentResolver contentResolver = getContentResolver();

Uri uri;
Cursor c;

uri = Uri.parse("content://sms/inbox");
c = contentResolver.query(uri, null, null, null, null);
c.moveToFirst();
map.put("sms_received", String.valueOf(c.getCount()));
c.close();

uri = Uri.parse("content://sms/sent");
c = contentResolver.query(uri, null, null, null, null);
c.moveToFirst();
map.put("sms_sent", String.valueOf(c.getCount()));
c.close();

uri = CallLog.Calls.CONTENT_URI;
c = contentResolver.query(uri, null, null, null, Calls.DEFAULT_SORT_ORDER);
c.moveToFirst();

long callsIncoming = 0, callOutgoing = 0;
while(!c.isAfterLast()){
int type = c.getInt(c.getColumnIndex(Calls.TYPE));

if(type == Calls.INCOMING_TYPE){
Log.d("Incoming", String.valueOf(c.getLong(c.getColumnIndex(Calls.DURATION))));
callsIncoming += c.getLong(c.getColumnIndex(Calls.DURATION));
}
else if(type == Calls.OUTGOING_TYPE){
Log.d("Outgoing", String.valueOf(c.getLong(c.getColumnIndex(Calls.DURATION))));
callOutgoing += c.getLong(c.getColumnIndex(Calls.DURATION));
}

c.moveToNext();
}

map.put("calls_incoming", String.valueOf(callsIncoming));
map.put("calls_outgoing", String.valueOf(callOutgoing));
c.close();

return map;
}



@Override
protected void onPostExecute(HashMap result) {

tvSMSReceived.setText(result.get("sms_received"));
tvSMSSent.setText(result.get("sms_sent"));
tvSMSCost.setText(String.format("PhP %.2f", Float.valueOf(result.get("sms_sent"))));
tvCallsIncoming.setText(formatTime(Long.valueOf(result.get("calls_incoming"))));
tvCallsOutgoing.setText(formatTime(Long.valueOf(result.get("calls_outgoing"))));
Double totalCallCost = Math.ceil(Double.valueOf(result.get("calls_outgoing")) / 60) * 7;
tvCallsCost.setText(String.format("PhP %.2f", totalCallCost));

svContainer.removeAllViews();
svContainer.addView(llContainer);
}
}

public static String formatTime(long time){;
long hours = time / 3600;
long minutes = (time % 3600) / 60;
long seconds = (time % 3600) % 60;
return String.format("%02d:%02d:%02d", hours, minutes, seconds);
}

public Object createDate(int i, int j, int k) {
return null;
}


}

 
Submit Your Answer
  |  
 
 
 

Preview :