在广播接收器中获得当前系统时间

时间:2014-11-27 09:05:56

标签: android datetime broadcastreceiver

我正在使用以下代码在BroadcastReceiver执行呼叫时获得时间,但在拨打电话后我总是得到相同的值。使用此代码时,与Activity一起使用时效果很好,但它在BroadcastReceiver中抛出相同的值。

@Override
public void onReceive(Context context, Intent intent) {
    SimpleDateFormat s = new SimpleDateFormat("hhmmss");
    String time= s.format(new Date());  
    System.out.println(time);
}

接收机

<receiver android:name="com.pairdroid.Information.OutCallLogger" >
    <intent-filter>
        <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
    </intent-filter>
</receiver>

2 个答案:

答案 0 :(得分:1)

您的时间格式字符串错误尝试替换为:

Format formatter = new SimpleDateFormat("hh:mm:ss a");

答案 1 :(得分:0)

试试这个。

String format = "HH:mm:ss";
SimpleDateFormat sdf =new SimpleDateFormat(format, Locale.US);
String callstarttime = sdf.format(Calendar.getInstance().getTime());
Toast.makeText(context, callstarttime, Toast.LENGTH_SHORT).show();