及增加权限:整个源码:pac..._安卓 程序默认开机启动并禁止锁屏怎么设置">
赞
踩
实现功能,开机后自动启动FirstActivity,并且不用解锁。才会 执行
Toast.makeText(arg0, "Boot Complete",Toast.LENGTH_LONG).show();
贴出源码:
主要是在配置文件中配置:
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
及增加权限:整个源码:
package="com.example.activitytest"
android:versionCode="1"
android:versionName="1.0" >
android:minSdkVersion="8"
android:targetSdkVersion="19" />
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
android:name=".FirstActivity"
android:label="this is my first Activity" >
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
增加BootUpReceiver 类,FirstActivity就是你要开启启动的活动;package com.example.activitytest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class BootUpReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Intent i = new Intent(context,FirstActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
要启动的类:FirstActivity.java
package com.example.activitytest;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class FirstActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//requestWindowFeature(Window.FEATURE_NO_TITLE);//要使用Menu 注释掉这句
getWindow().setFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD,
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);//开机不锁屏 设置
setContentView(R.layout.first_layout);
Button button1 = (Button)findViewById(R.id.button_1);
button1.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
//Toast.makeText(FirstActivity.this,"you click button1", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.baidu.com"));//setData,设置data标签 主要是tel,这个不能少
startActivity(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.main, menu);
return true;//允许创建的菜单显示出来
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case R.id.add_item:
Toast.makeText(this, "you click add",Toast.LENGTH_SHORT).show();
break;
case R.id.remove_item:
Toast.makeText(this, "you click remove", Toast.LENGTH_SHORT).show();
break;
default: break;
}
return true;
}
}
PS: 开启启动这个app,需要解屏后稍等一会,才能启动;启动APP需要一定时间。
其实还可以在开机启动时设置不锁屏。
getWindow().setFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD,
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);//开机不锁屏 设置 放在这个前面。
setContentView(R.layout.first_layout);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。