主要参考:http://life173.blog.51cto.com/2035365/543916/
实现功能,开机后自动启动FirstActivity,并且不用解锁。
贴出源码:
主要是在配置文件中配置:
<receiver android:enabled="true" android:name=".BootUpReceiver" android:permission="android.permission.RECEIVE_BOOT_COMPLETED"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </receiver>
及增加权限: <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 整个源码: <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.activitytest" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".FirstActivity" android:label="this is my first Activity" > <intent-filter > <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> <receiver android:enabled="true" android:name=".BootUpReceiver" android:permission="android.permission.RECEIVE_BOOT_COMPLETED"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </receiver> </application> </manifest>
增加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);
时间: 2024-10-10 18:17:49