您的位置:首页 > 移动开发 > Android开发

android 定时关机需root权限

2016-09-30 00:00 10 查看
权限

<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"/>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息