Android系統啟動時間(不是系統當前時間)的獲取
阿新 • • 發佈:2019-01-28
1》.查裡很多資料,並沒有找到直接獲取系統啟動時間(不是系統當前時間)的ApI;
思路轉換為:接收系統的開機廣播,在廣播裡面獲取當前的時間;
2》.注意幾點:
1.BootUPReceiver廣播類建立,這個廣播只能靜態註冊;
2.在清單檔案註冊廣播,要給開機廣播許可權
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
3.在OnReceive方法裡面獲取系統當前時間,再將時間儲存到sharedPreference裡面
4.在MainActivity裡面獲取sharedPreference裡面資料,展示到TextView裡面
5.注意:先安裝應用,然後重啟手機,eclipse裡面才能看到BootUpReceiver裡面要列印資訊;再開啟應用,eclipse裡面才能看到MainActivity裡面要列印資訊。(一定要重啟!!!)
3》
1.BootUpReceiver類的建立:
package com.zhc.bootupreceiver; import java.text.SimpleDateFormat; import java.util.Date; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.widget.Toast; public class BootUpReceiver extends BroadcastReceiver {//這種接受開機廣播的,一定要靜態註冊,這樣應用還沒執行起來時也照樣能夠接收到開機廣播 private SharedPreferences sharedPreferences; private Editor editor; private SimpleDateFormat formatter; @Override public void onReceive(Context context, Intent intent) {//注意onReceive方法裡面不能執行大量邏輯操作和耗時操作 Toast.makeText(context, "Boot complete",Toast.LENGTH_SHORT ).show(); System.out.println("------------22222222222------------"); // 獲取當前時間 formatter = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss "); Date curDate = new Date(System.currentTimeMillis()); String date = formatter.format(curDate); /** * 儲存資料到sp裡面 */ sharedPreferences = context.getSharedPreferences("BootUpTime", Context.MODE_PRIVATE); editor = sharedPreferences.edit(); editor.putString("timekey", date); editor.commit();// 別忘了提交 } }
2.MainActivity:
package com.zhc.bootupreceiver; import java.util.List; import android.os.Bundle; import android.app.Activity; import android.app.ActivityManager; import android.app.ActivityManager.RunningAppProcessInfo; import android.content.Context; import android.content.SharedPreferences; import android.util.Log; import android.widget.TextView; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView view = (TextView) findViewById(R.id.tv); /** * 從sp裡面獲取資料 */ SharedPreferences pre=getSharedPreferences("BootUpTime", Context.MODE_PRIVATE); String time = pre.getString("timekey","" ); System.out.println("111111111111111111111"+time); view.setText(time); } }
3.清單檔案:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zhc.bootupreceiver"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name=".BootUpReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</receiver>
<activity
android:name="com.zhc.bootupreceiver.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>