监听系统开机广播并启动Activity

功能:系统开机弹出对话框,提示用户信息,如果用户点击“do not show again",则下次开机就不用显示了。


分析:(1)监听系统开机广播,继承BroadcastReceiver类,在onReceive方法中,使用Intent跳转到Activity,同时在AndroidManifest.xml配置相关信息: 

                          1)为广播设置监听Action:     

        <receiver android:name="com.android.bird.BirdBroadCastReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" >
                </action>
            </intent-filter>
        </receiver>


                  2)添加权限:      

<span style="font-family:SimHei;font-size:14px;"><uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /></span>


     (2)在Activity中,在AndroidManifest.xml文件中设置主题为:android:theme="@android:style/Theme.Holo.Light.Dialog",通过这里不能使用AlertDialog,否则Activity本身也是一个Dialog,所以直接将Activity定义为Dialog风格即可同时自定义布局。

    

      (3)如果在诸多的应用中,涉及到了某个功能只要实现一次,例如本次,如果用户点击“do not show again",则下次开机就不用显示了,这里可以使用SharedPreferences

           1)通过context.getSharedPreferences("BIRD_SHOW_BOOT_REMIND",0);获取SharedPreferences的一个实例,调用实例的get***方法获取KEY值;如果是修改KEY值则通过实例的edit方法得到SharedPreferences.Editor实例,然后调用put***方法修改KEY值,最后记得需要调用commit方法才会修改。

问题:(1)在广播类中,如果直接通过Intent启动Activity会报错,需要为Intent增加一个Flag:intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);


源码:扩展广播类:

public class BirdBroadCastReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		SharedPreferences preferences = context.getSharedPreferences("BIRD_SHOW_BOOT_REMIND",0);
		boolean flag = preferences.getBoolean("KEY", true);
		if(flag){
			Intent tintent = new Intent(context, MainActivity.class);
			tintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			context.startActivity(tintent);
		}
	}
}

主Activity类:

<span style="font-size:14px;">public class MainActivity extends Activity {

	private Button show_again = null;
	private Button cancle = null;
	private SharedPreferences preferences = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		show_again = (Button) findViewById(R.id.again);
		cancle = (Button) findViewById(R.id.cancle);
		preferences = getSharedPreferences("BIRD_SHOW_BOOT_REMIND", 0);
		show_again.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				SharedPreferences.Editor edit = preferences.edit();
				edit.putBoolean("KEY", false);
				edit.commit();
				finish();
			}
		});
		cancle.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}
}</span>
布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dip"
        android:text="@string/reminder_message"
        android:textSize="20dip" />

    <View
        android:id="@+id/black"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_below="@id/view"
        android:background="@android:color/darker_gray" />

    <LinearLayout
        style="?android:attr/buttonBarStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/black"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/again"
            style="?android:attr/buttonBarButtonStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/show_again" />

        <Button
            android:id="@+id/cancle"
            style="?android:attr/buttonBarButtonStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/accept" />
    </LinearLayout>

</RelativeLayout>


AndroidManifest.xml文件:

<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.bird"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="18" />

    <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="@android:style/Theme.Holo.Light.Dialog" >
        <activity
            android:name="com.android.bird.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

            </intent-filter>
        </activity>

        <receiver android:name="com.android.bird.BirdBroadCastReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" >
                </action>
            </intent-filter>
        </receiver>
    </application>

</manifest></span>





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值