(记住一定要在源码下编译生成apk,也就是放在源码的package/app下编译!!!)

先看一下简单的图形界面吧,比较简单的实现定时关机!!!(我的上传资源中有整个工程:注:设定的关机时间是24小时制的)

当然我们是在eclipse下开发的,那么我们先建一个工程Shutdown(当然可以随便取),然后就是包名,类名什么的。。。这就不多说了,这里先看一下上面那个时钟的

代码:analogClock=(AnalogClock)findViewById(R.id.anolag_clock);
new Thread(){
public void run() {
try {
while(true)
{
Thread.sleep(1000);
tick++;
Message msg = Message.obtain();
msg.arg1 = tick;
handler.sendMessage(msg);
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}.start();
handler=new Handler(){
public void handleMessage(Message msg) {
Calendar calendar=Calendar.getInstance();
int h=calendar.getTime().getHours();
int m=calendar.getTime().getMinutes();
int hour=h, minute = m;
tick=msg.arg1;
minute+= tick/60;
tick =tick%60;
hour+= minute /60;
minute=minute%60;
String str =" NOW TIME "+ MessageFormat
.format("{0,number,0}:{1,number,00}", hour%24,minute);
text.setText(str);

super.handleMessage(msg);
}
};
显示当前的时钟。

save button响应事件

class button_saveListener implements OnClickListener
{

@Override
public void onClick(View v) {
SharedPreferences sharedPreferences=getSharedPreferences("time",Activity.MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
if(edit_hour.getText().toString().equals("")||edit_minute.getText().toString().equals(""))
Toast.makeText(ShutdownActivity.this, "have no shutdown time", Toast.LENGTH_SHORT).show();
else
{
editor.putInt("hour",Integer.parseInt(edit_hour.getText().toString()));
editor.putInt("minute",Integer.parseInt(edit_minute.getText().toString()));
editor.commit();
Toast.makeText(ShutdownActivity.this, "save success", Toast.LENGTH_SHORT).show();
}
text_time.setText("SHUTDOWN TIME :"+Integer.parseInt(edit_hour.getText().toString())+":"+Integer.parseInt(edit_minute.getText().toString()));
Intent intent =new Intent();
intent.setClass(ShutdownActivity.this, ServiceActivity.class);//要建一个Service
startService(intent);
}
}

我们再来看看Service类:

public void onCreate() {

SharedPreferences myshaPreferences=getSharedPreferences("time",Activity.MODE_PRIVATE);
hour=myshaPreferences.getInt("hour", -1);
minute=myshaPreferences.getInt("minute", -1);
Thread thread=new Thread(){
public void run()
{
while(true)
{
Calendar calendar=Calendar.getInstance();
h=calendar.getTime().getHours();
m=calendar.getTime().getMinutes();

if(h==hour&&m==minute)
{
Intent newIntent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);//这里是会报错的,所以不能在eclipse下编译,要在源码下编译(下面会介绍)
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(newIntent);
break;
}
try {
Thread.sleep(1000);
} catch (Exception e)
{
e.printStackTrace();
}
}
}
};
thread.start();
super.onCreate();
}

在Manifast.xml文件中添加如下权限:<uses-permission android:name="android.permission.SHUTDOWN"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>"
还是贴出来吧:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="jxnu.shutdown"
android:versionCode="1"
android:versionName="1.0"
>
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.SHUTDOWN"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>"
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".ShutdownActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".ServiceActivity" android:label="@string/app_name" android:enabled="true"></service>
<receiver android:name=".ShutdownBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED">
</action>
</intent-filter>
</receiver>
</application>

建立一个 BroadcastReceiver来接收广播android.intent.action.BOOT_COMPLETED(每次开机就启动service)

public class ShutdownBroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
Intent newIntent = new Intent(context,ServiceActivity.class);
context.startService(newIntent);
}
}
}
大概差不多了,但是要将你的工程放到源码的package/app中,另外还得建一个Android.mk文件和default.properties文件,然后就是编译了,生成的Shutdown.apk会在out/.../system/app下,可以拿来安装。


具体代码在我上传的资源中有整个工程,想要学习的同学可以借鉴一下.



更多相关文章

  1. Android工程的编译过程
  2. Android实战技巧之四十四:Hello,Native!
  3. Android(安卓)7.1.1系统源码下载、编译、刷机-Nexus 6实战
  4. Android(安卓)Studio 3.0 新功能全面解析和旧项目适配问题
  5. android makefile(android.mk)分析(序)
  6. Android系统下C语言hello world
  7. 安卓-编译过程简述
  8. android仿iPhone滚轮控件实现及源码分析(二)
  9. Mac下配置Android(安卓)NDK环境并搭建Cocos2d-x环境并Eclipse正

随机推荐

  1. 获取Android应用程序的签名
  2. Android(安卓)Dialog 创建上下文菜单
  3. android 计算器,纯手打,功能实现了,bug可能
  4. android创立输入对话框
  5. android 进度对话框 圈
  6. android Animation介绍
  7. Android单击屏幕获得坐标,屏幕多点触摸测
  8. Android学习笔记(十九)
  9. 深入理解Android(安卓)Notifiction机制
  10. Android(安卓)RecyclerView 间距全适配