由於手機常常沒電
於是寫個APP通知一下別人

MainActivity.java
- package lin.powercall;
- import android.app.Activity;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.content.IntentFilter;
- import android.net.Uri;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.Menu;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.CompoundButton;
- import android.widget.CompoundButton.OnCheckedChangeListener;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.ToggleButton;
- public class MainActivity extends Activity {
-
- MyBatteryReceiver mbr = null;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- mbr = new MyBatteryReceiver();
- final TextView tv2 =(TextView)findViewById(R.id.tv2);
- ToggleButton tb = (ToggleButton)findViewById(R.id.tb);
-
- tb.setOnCheckedChangeListener(new OnCheckedChangeListener(){
- @Override
- public void onCheckedChanged(CompoundButton buttonView,
- boolean isChecked) {
- // TODO 自動產生的方法 Stub
- if (isChecked) {
-
- IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
- registerReceiver(mbr,filter);
- } else {
- unregisterReceiver(mbr);
- TextView tv1 = (TextView)findViewById(R.id.tv1);
- tv1.setText(null);
- tv2.setText(null);
- }
- }
- });
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
-
- private class MyBatteryReceiver extends BroadcastReceiver {
- @Override
- public void onReceive(Context context, Intent intent) {
- // TODO 自動產生的方法 Stub
- String num = "";
- String percentstring = "";
- int percentint=100;
- int current = intent.getExtras().getInt("level");
- int total = intent.getExtras().getInt("scale");
- int percent = current*100/total;
-
- TextView tv =(TextView)findViewById(R.id.tv1);
- TextView tv2 =(TextView)findViewById(R.id.tv2);
- EditText et1 = (EditText)findViewById(R.id.et1);
- EditText et2 = (EditText)findViewById(R.id.et2);
- percentstring = et2.getText().toString();
- Log.d("設定警示電量", percentstring);
- tv.setText("目前電池電量:"+percent+"% ‧");
- tv2.setText("設定警示電量:"+percentstring+"% ‧");
- percentint = Integer.parseInt(percentstring);
-
-
- if(percent<=percentint){
- num = et1.getText().toString();
- Intent dial = new Intent();
- dial.setAction("android.intent.action.CALL");
- dial.setData(Uri.parse("tel://"+num));
- startActivity(dial);
- }
- }
-
- }
- }
複製代碼
activity_main.xml
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:id="@+id/LinearLayout1"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- tools:context=".MainActivity" >
-
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content" >
- <TextView
- android:id="@+id/textView1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="電話號碼:" />
- <EditText
- android:id="@+id/et1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:ems="10" >
- <requestFocus />
- </EditText>
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content" >
- <TextView
- android:id="@+id/textView2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="電源低於%警示:" />
- <EditText
- android:id="@+id/et2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:ems="10" />
- </LinearLayout>
- <TextView
- android:id="@+id/tv1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="24sp"
- android:text="請啟動電池監控" />
- <TextView
- android:id="@+id/tv2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- <ToggleButton
- android:id="@+id/tb"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:textOn="電源打開"
- android:textOff="電源關閉"
- />
- <!-- ToggleButton 電源按鈕控制 -->
- </LinearLayout>
複製代碼 AndroidManifest.xml 加入
- <intent-filter>
- <action android:name="android.intent.action.CALL_BUTTON" />
- <category android:name="android.intent.category.DEFAULT" />
- </intent-filter>
複製代碼- <uses-permission android:name="android.permission.CALL_PHONE"/>
複製代碼 這樣就可以了
原始碼: powercall.rar
|