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

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