How to show toast status when phone got missed call that just happened?
I tried to figure out CallLog and event that just happening on phone, when it got phone call or missed call.
Is it possible when I run my app and there's missed call, my app wi开发者_如何转开发ll show toast status?
Similar question on anddev
http://www.anddev.org/other-coding-problems-f5/miss-call-alert-in-android-t3959.html
edit: as requested in comments
import java.text.DateFormat;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.CallLog;
import android.util.Log;
public class CallLogActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String[] strFields = {
android.provider.CallLog.Calls.CACHED_NAME, android.provider.CallLog.Calls.NUMBER,
android.provider.CallLog.Calls.DATE, android.provider.CallLog.Calls.TYPE
};
String strOrder = android.provider.CallLog.Calls.DATE + " DESC";
Cursor mCallCursor = getContentResolver().query(android.provider.CallLog.Calls.CONTENT_URI,
strFields, null, null, strOrder);
if (mCallCursor.moveToFirst()) {
do {
boolean missed = mCallCursor.getInt(mCallCursor.getColumnIndex(CallLog.Calls.TYPE)) == CallLog.Calls.MISSED_TYPE;
if (missed) {
String name = mCallCursor.getString(mCallCursor
.getColumnIndex(CallLog.Calls.CACHED_NAME));
String number = mCallCursor.getString(mCallCursor
.getColumnIndex(CallLog.Calls.NUMBER));
String time = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG)
.format(mCallCursor.getLong(mCallCursor
.getColumnIndex(CallLog.Calls.DATE)));
Log.d("PhoneLog", "You have a missed call from " + name + " on " + number
+ " at " + time);
}
} while (mCallCursor.moveToNext());
}
}
}
make sure you have the
<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
精彩评论