package com.hyf.service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class RecordisBootReciever extends BroadcastReceiver {
public static int shutdowntimes;
public static int startuptimes;
private static final String TAG="RecordShutdownTimes";
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())){
startuptimes++;
}
shutdowntimes=(shutdowntimes==0)?0:startuptimes-1;
}
}
------------------------------------------------------------------
package com.hyf.test;
import com.hyf.service.RecordisBootReciever;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class ShutDownTimesActivity extends Activity {
private TextView startuptimes;
private TextView shutdowntimes;
Button startuptimesButton;
Button shutdowntimesButton;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startuptimesButton=(Button)findViewById(R.id.startuptimesButton);
shutdowntimesButton =(Button)findViewById(R.id.shutdowntimesButton);
startuptimes= (TextView)findViewById(R.id.startuptimes);
shutdowntimes=(TextView)findViewById(R.id.shutdowntimes);
startuptimesButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startuptimes.setText(RecordisBootReciever.startuptimes);
setContentView(R.layout.main);
}
});
shutdowntimesButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
shutdowntimes.setText(RecordisBootReciever.shutdowntimes);
setContentView(R.layout.main);
}
});
}
}
---------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
>
<Button
android:id="@+id/startuptimesButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/startuptimesButton"
/>
<TextView
android:id="@+id/startuptimes"
android:layout_width="150dip"
android:layout_height="wrap_content"
android:background="#aaaa00"
/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
>
<Button
android:id="@+id/shutdowntimesButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/shutdowntimesButton"
/>
<TextView
android:id="@+id/shutdowntimes"
android:layout_width="150dip"
android:layout_height="wrap_content"
android:background="#aaaa00"
/>
</LinearLayout>
</LinearLayout>