android 定时关机需root权限

权限

<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

 

AlarmManager定时任务

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import com.example.administrator.shutdowneasy.utils.SharePreferenceUtil;

import java.util.Calendar;

public class ShutDownInTime extends AppCompatActivity {

    private SharePreferenceUtil saveUtil;
    private TimePicker time=null;
    private TextView msg=null;
    private Button set=null;
    //private Button delete=null;
    private AlarmManager alarm=null;
    private int hourOfDay=0;
    private int minute=0;
    private Calendar cale=Calendar.getInstance();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shut_down_in_time);
        saveUtil = new SharePreferenceUtil(this,"Time");

        alarm=(AlarmManager)super.getSystemService(ALARM_SERVICE);
        set=(Button)super.findViewById(R.id.set);
        msg=(TextView)super.findViewById(R.id.msg);
        time=(TimePicker)super.findViewById(R.id.time);
        //delete=(Button)super.findViewById(R.id.delet);
        set.setOnClickListener(new setImp());
        //delete.setOnClickListener(new deletImp());
        time.setIs24HourView(true);
        time.setOnTimeChangedListener(new timeChange());

    }
    private class timeChange implements TimePicker.OnTimeChangedListener {

        @Override
        public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
            cale.setTimeInMillis(System.currentTimeMillis());
            cale.set(Calendar.HOUR_OF_DAY,hourOfDay);
            cale.set(Calendar.MINUTE,minute);
            cale.set(Calendar.SECOND,0);
            cale.set(Calendar.MILLISECOND, 0);
            ShutDownInTime.this.hourOfDay=hourOfDay;
            ShutDownInTime.this.minute=minute; //set time is long

        }

    }

    private class setImp implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            Intent it=new Intent(ShutDownInTime.this,AlarmBoast.class);
            it.setAction("com.example.administrator.shutdowneasy");
            PendingIntent sender=PendingIntent.getBroadcast(ShutDownInTime.this, 0, it,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            ShutDownInTime.this.alarm.set(AlarmManager.RTC_WAKEUP,ShutDownInTime.this.cale.getTimeInMillis(),sender);

            ShutDownInTime.this.msg.setText("您设置的自动关机时间是:"+ShutDownInTime.this.hourOfDay+":"+ShutDownInTime.this.minute);
            Toast.makeText(ShutDownInTime.this, "自动关机已设置成功", Toast.LENGTH_LONG).show();
            ShutDownInTime.this.finish();
        }
    }
}

广播内容:

public class AlarmBoast extends BroadcastReceiver {
    public AlarmBoast() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.
        try {
            Process process = Runtime.getRuntime().exec("su");
            DataOutputStream out = new DataOutputStream(
                    process.getOutputStream());
            out.writeBytes("poweroff -f\n");//可以为reboot -p\n
            out.writeBytes("exit\n");
            out.flush();
        } catch (IOException e) {
            new AlertDialog.Builder(MainActivity.this).setTitle("Error").setMessage(
                    e.getMessage()).setPositiveButton("OK", null).show();
        }
    }
}

mainfest:

       <receiver
            android:name=".AlarmBoast"
            android:enabled="true"
            android:process=":remote">
            <intent-filter>
                <action android:name="com.example.systemc" />
            </intent-filter>
        </receiver>

xml:

    <TimePicker android:id="@+id/time"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
    <TextView android:id="@+id/msg"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/time"
        android:text="没有设置关机时间"/>
    <Button android:id="@+id/set"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="设置关机时间"
        android:layout_below="@id/msg"/>

 

转载于:https://my.oschina.net/u/2520777/blog/753781

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值