Android 开机自启或干其他事情
2016-07-18 11:23
337 查看
目录结构如下,这是下的一个demo(错误百出,是不是故意整人呢==!),然后改了改测试成功后印象更深刻。
首先一个receiver是少不了的,代码如下:
PS:要注意他的mainfest中包名和放java的包名不一样.本以为下的demo都是调好的结果,没有运行不起来,改好了;重启又起不来,好再调,还不如自己粘个调试呢!!!
package cn.etzmico; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; public class BootReceiver extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) { Log.v("BootReceiver", "system boot completed"); // 启动应用方式1: /* * Intent intent1 = context * .getPackageManager().getLaunchIntentForPackage("cn.atomicc"); * context.startActivity(intent1); */ // 启动应用方式2: /* * Intent newIntent = new Intent(context, cn.etzmico.AutoRun.class); * newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); * context.startActivity(newIntent); */ // 启动service: Intent service = new Intent(context, OlympicsService.class); context.startService(service); } } }然后配置下mainfest.1.权限2.receiver注册:(注意包名)
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.atomicc" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="15" android:targetSdkVersion="21" /> <application android:icon="@drawable/icon" android:label="@string/app_name" > <receiver android:name="cn.etzmico.BootReceiver" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </receiver> <service android:name="cn.etzmico.OlympicsService" android:exported="false" > </service> <activity android:name="cn.etzmico.AutoRun" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" > </uses-permission> </manifest>
然后就可以测一个启动应用的了。启动服务也如出一辙,注册个service启动一下:我这里粘了一个intentservice的例子:
package cn.etzmico; import android.app.IntentService; import android.content.Intent; import android.os.Handler; import android.util.Log; import android.widget.Toast; public class OlympicsService extends IntentService { public OlympicsService() { super("OlympicsService"); } String ex = ""; private Handler mHandler = new Handler() { public void handleMessage(android.os.Message msg) { Log.v("resulttt", "程序启动了··········································"); Toast.makeText(OlympicsService.this, "-e " + ex, Toast.LENGTH_LONG).show(); } }; @Override public int onStartCommand(Intent intent, int flags, int startId) { ex = "服务启动了"; return super.onStartCommand(intent, flags, startId); } @Override protected void onHandleIntent(Intent intent) { try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } mHandler.sendEmptyMessage(0); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } } }
还有其他问题可以参考一下:http://www.xuebuyuan.com/1372262.html(不是新手反而有时候容易被坑,所以不成功时可以这样检查一遍)
Save
相关文章推荐
- Android 绘图(二) Canvas
- Android学习一(windows安装Git)
- Android DownloadManager 的使用
- android系统的启动逻辑01
- Android Studio 项目目录结构 英文版
- android跨进程通信(IPC):使用AIDL
- Android 修改厂商信息
- Android随VierPager滑动的导航栏
- Android解析Json数据之FastJson解析
- Error:(1, 0) Plugin is too old, please update to a more recent version, or set ANDROID_DAILY_OVERRID
- 最新基于高德地图的android进阶开发(2)小试牛刀
- Android中ProgressBar自定义进度条的高度、颜色、圆角
- 【腾讯Bugly干货分享】浅谈Android自定义锁屏页的发车姿势
- Android 防止popup window遮挡软键盘
- Android开发中发送Get请求的实例
- Android Studio 的常用快捷键
- android:configChanges="orientation|keyboardHidden"的使用
- Android绘图——圆圈+水波
- 【腾讯Bugly干货分享】浅谈Android自定义锁屏页的发车姿势
- Android开发之RecyclerView的使用全解