关于广播的基础知识参看我的这篇文章http://blog.csdn.net/jdfkldjlkjdl/article/details/43017091
下面是一个强制用户线下的例子。仅供参考。
强制下线功能需要先关闭掉所有的活动,然后回到登陆界面。
1.首先创建一个项目BroadcastBestPractice,并新建ActivityController类,
- package com.example.broadcastbestpractice;
-
- import java.util.ArrayList;
- import java.util.List;
-
- import android.app.Activity;
-
-
-
-
-
-
-
-
- public class ActivityController {
-
- public static List<Activity> activities = new ArrayList<Activity>();
- public static void addActivity(Activity activity){
- activities.add(activity);
- }
-
- public static void removeActivity(Activity activity){
- activities.remove(activity);
- }
-
- public static void finishAll(){
- for(Activity activity:activities){
- if(!activity.isFinishing()){
- activity.finish();
- }
- }
- }
- }
2.创建BaseActivity作为所有活动的父类。
- package com.example.broadcastbestpractice;
-
- import android.app.Activity;
- import android.os.Bundle;
-
-
-
-
-
-
-
-
- public class BaseActivity extends Activity {
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
-
- super.onCreate(savedInstanceState);
- ActivityController.addActivity(this);
- }
-
- @Override
- protected void onDestroy() {
-
- super.onDestroy();
- ActivityController.removeActivity(this);
- }
- }
3.创建一个登陆界面的布局文件login.xml,此处主要实现功能,界面很简单。
- <?xml version="1.0" encoding="utf-8"?>
- <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:stretchColumns="1" >
-
- <TableRow>
-
- <TextView
- android:layout_height="wrap_content"
- android:text="Account:" />
-
- <EditText
- android:id="@+id/accountEdit"
- android:layout_height="wrap_content"
- android:hint="input your account" />
- </TableRow>
-
- <TableRow>
-
- <TextView
- android:layout_height="wrap_content"
- android:text="Password:" />
-
- <EditText
- android:id="@+id/passwordEdit"
- android:layout_height="wrap_content"
- android:inputType="textPassword" />
- </TableRow>
-
- <TableRow>
-
- <Button
- android:id="@+id/login"
- android:layout_height="wrap_content"
- android:layout_span="2"
- android:text="Login"
- />
- </TableRow>
-
- </TableLayout>
4.写登陆活动
LoginActivity.java,并让其继承自BaseActivity.java.
- package com.example.broadcastbestpractice;
-
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
-
-
-
-
-
-
-
-
- public final class LoginActivity extends BaseActivity {
-
- private EditText etAccount,etPass;
- private Button btnLogin;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
-
- super.onCreate(savedInstanceState);
- setContentView(R.layout.login);
- etAccount = (EditText)findViewById(R.id.accountEdit);
- etPass = (EditText)findViewById(R.id.passwordEdit);
- btnLogin = (Button)findViewById(R.id.login);
-
- btnLogin.setOnClickListener(new OnClickListener() {
-
- @Override
- public void onClick(View arg0) {
-
- String account = etAccount.getText().toString();
- String password = etPass.getText().toString();
-
- if(account.equals("xu") && password.equals("123")){
- Intent intent = new Intent(LoginActivity.this,MainActivity.class);
- startActivity(intent);
- finish();
- }else{
- Toast.makeText(getApplicationContext(), "login failure", Toast.LENGTH_LONG).show();
- }
- }
- });
- }
- }
上提到的MainActivity可以理解成程序登陆成功后进入的程序主界面,此处我们不需要很花哨的功能,只需要在MainActivity中添加强制下线功能即可。故修改activity_main.xml在其中添加一个按钮
5.修改activity_main.xml文件
- <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"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- tools:context=".MainActivity" >
-
- <Button
- android:id="@+id/force_offline"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Send force offline broadcast" />
-
- </RelativeLayout>
6.修改MainActivity代码,在该代码中实现强制下线功能
- package com.example.broadcastbestpractice;
-
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
-
-
-
-
-
-
-
-
- public class MainActivity extends Activity {
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Button btnForceOffline = (Button)findViewById(R.id.force_offline);
- btnForceOffline.setOnClickListener(new OnClickListener() {
-
- @Override
- public void onClick(View arg0) {
-
-
- Intent intent = new Intent("com.example.broadcastbestpractice.FORCE_OFFLINE");
- sendBroadcast(intent);
-
- }
- });
- }
-
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
-
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
-
- }<strong>
- </strong>
上面的程序很简单,但是有一个重点,就是我们在按钮里面发送了一条广播,广播的值为com.example.broadcastbestpractice.FORCE_OFFLINE;这条广播是用于通知程序强制用户下线的。也就是说强制用户下线程序并不是卸载MainActivity中,而是应该写在接收这条广播的接收器里面,这样强制下线的功能就不会依附于任何界面,不管在程序的那个界面,只要发送一条这样的广播,就可以完成强制下线的操作了。name毫无疑问,接下来我们就需要创建一个广播接收器了。
7.新建接收器ForceOfflineReceiver,继承自BroadcastReceiver
- package com.example.broadcastbestpractice;
-
- import android.app.AlertDialog;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.view.WindowManager;
-
-
-
-
-
-
-
-
- public class ForceOfflineReceiver extends BroadcastReceiver {
-
- @Override
- public void onReceive(final Context context, Intent intent) {
-
- AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
- dialogBuilder.setTitle("Warning");
- dialogBuilder.setMessage("You are forced to be offline.Please try to login again.");
- dialogBuilder.setCancelable(false);
- dialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
-
- @Override
- public void onClick(DialogInterface dialog, int which) {
-
-
- ActivityController.finishAll();
- Intent intent = new Intent(context,LoginActivity.class);
-
- intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- context.startActivity(intent);
- }
- });
- AlertDialog alertDialog = dialogBuilder.create();
-
- alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
- alertDialog.show();
- }
-
- }<strong>
- </strong>
这次onReceive()方法里可不再是仅仅弹出一个Toast,而是加入较多的代码,首先使用AlertDialog.Builder来构建一个对话框,注意这里一定要调用setCancelable()方法将对话框设置为不可取消,否则用户按一下back键就可以关闭对话框继续使用程序了。然后使用setPositiveButton()方法来给对话框注册确定按钮,当用户点击了确定按钮时,就调用ActivityController的finishAll()方法来销毁所有的活动,并重新启动LoginActivity这个活动,另外,由于我们在广播接收器里面启动活动的,因为一定要给Intent加入FLAG_ACTIVITY_NEW_TASK这个标志。最后还要把对话框的类型设为TYPE_SYSTEM_ALERT,不然它将无法在广播接收器里弹出。这样的话,所有强制下线的逻辑就已经基本完成,接下来我们还需要对AndroidManifest.xml文件进行配置。
8.配置AndroidManifest.xml文件
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.broadcastbestpractice"
- android:versionCode="1"
- android:versionName="1.0" >
-
- <uses-sdk
- android:minSdkVersion="8"
- android:targetSdkVersion="18" />
- <span style="color:#cc0000;"><uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/></span>
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name="com.example.broadcastbestpractice.LoginActivity"
- android:label="@string/app_name" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <activity
- android:name="com.example.broadcastbestpractice.MainActivity" >
- </activity>
- <span style="color:#cc0000;"> <receiver android:name=".ForceOfflineReceiver">
- <intent-filter>
- <action android:name="com.example.broadcastbestpractice.FORCE_OFFLINE" />
- </intent-filter>
- </receiver></span>
- </application>
-
- </manifest>
上面的配置文件需要注意的几点:1.由于在ForceOfflineReceiver里弹出了一个系统级别的对话框,因此必须要声明android.permission.SYSTEM_ALERT_WINDOW权限,然后对LoginActivity进行注册,并把它设置为主活动,然后再对ForceOfflineReceiver进行注册,并指定它接收broadcastbestpractice.FORCE_OFFLINE这条广播。
程序源代码:http://pan.baidu.com/s/1i3w0f57