1
我試圖找到使用服務和broadcastreceiver最後一次android重新啓動的時間。我的「廣播」類應該記錄重新啓動後的時間。在「OnCreate」方法計算時間,但它不起作用。如何找到上次重新啓動android的時間?
我在做什麼錯?
我的 「服務器」 類:
public class Server extends Service{
IBinder mBinder = new LocalBinder();
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
public class LocalBinder extends Binder {
public Server getServerInstance() {
return Server.this;
}
} }
我的 「客戶」 類:
public class Client extends Activity {
boolean mBounded;
Server mServer;
TextView text;
Button button;
Broadcast bc = new Broadcast();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text = (TextView)findViewById(R.id.text);
button = (Button) findViewById(R.id.button);
Intent mIntent = new Intent(this, Server.class);
bindService(mIntent, mConnection, BIND_AUTO_CREATE);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
text.setText(String.valueOf(System.currentTimeMillis() - bc.t));
}
});
}
@Override
protected void onStart() {
super.onStart();
Intent mIntent = new Intent(this, Server.class);
bindService(mIntent, mConnection, BIND_AUTO_CREATE);
}
ServiceConnection mConnection = new ServiceConnection() {
public void onServiceDisconnected(ComponentName name) {
Toast.makeText(Client.this, "Service is disconnected", Toast.LENGTH_LONG).show();
mBounded = false;
mServer = null;
}
public void onServiceConnected(ComponentName name, IBinder service) {
Toast.makeText(Client.this, "Service is connected", Toast.LENGTH_LONG).show();
mBounded = true;
LocalBinder mLocalBinder = (LocalBinder)service;
mServer = mLocalBinder.getServerInstance();
}
};
@Override
protected void onStop() {
super.onStop();
if(mBounded) {
unbindService(mConnection);
mBounded = false;
}
}}
「廣播」 類:
public class Broadcast extends BroadcastReceiver {
public long t;
@Override
public void onReceive(Context context, Intent intent) {
t = System.currentTimeMillis();
Intent serviceLauncher = new Intent(context, Server.class);
context.startService(serviceLauncher);
}}
清單:
<receiver android:name="com.example.serv.Broadcast" android:enabled="true" android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:enabled="true" android:name=".Server"/>
重新啓動後開始計時的任務。不要使用系統功能 – user3473445
啊,你應該提到這是一項家庭作業。 – ozbek