Android : How can I get Emulator date and time via code?

on Monday, August 11, 2014


How can I get Emulator date and time (ie. By changing or updating manually : Settings -> Date & Time). So I should be able to display it using toast or textview?


I should be able to get emulator's date and time via code when I change manually in settings.


I have tried by using action.DATE_CHANGED but broadcast receiver is not fired. I mean its not listening. May I know what mistake I have done here?


In MainActivity.java,


I have used below code :



public class BatteryIndicatorActivity extends Activity {
private PendingIntent mDateChangeSender;
private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context c, Intent i) {

if ("android.intent.action.DATE_CHANGED".equals(i.getAction())) {

Intent intent=new Intent(c,BatteryIndicatorActivity.class);
c.startService(intent);

String extractDate=c.startService(intent).toString();
System.out.println("Date changedNew is:"+ extractDate);

TextView tv = (TextView) findViewById(R.id.textfield);
tv.setText("TextView Date is: " + extractDate);
Toast.makeText(BatteryIndicatorActivity.this,"Toast Date changing is:"+extractDate,Toast.LENGTH_LONG).show();

}

else {
Toast.makeText(BatteryIndicatorActivity.this,"It fails to listen a broadcast:",Toast.LENGTH_LONG).show();

}

}

};

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
registerReceiver(mBatInfoReceiver, new IntentFilter(
Intent.ACTION_DATE_CHANGED));

}
}


In Manifest file,



<activity
android:name=".BatteryIndicatorActivity"
android:label="@string/title_activity_main" >


<intent-filter>
<action android:name="android.intent.action.MAIN" />

<receiver android:name="Interval" >
<intent-filter>
<action android:name="android.intent.action.DATE_CHANGED" />
</intent-filter>
</receiver>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>


Please can anyone suggest me? Atleast can anyone tell what mistake I have done here??


0 comments:

Post a Comment