先把所有的代码贴出来然后一个一个复习
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.admin.broadcastbestpracticw"> <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".LoginActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".MainActivity"> </activity> <receiver android:name=".ForceOfflineReceiver"> <intent-filter> <action android:name="com.example.broadcastbewspractice.FORCE_OFFLINE"/> </intent-filter> </receiver> </application> </manifest>
package com.example.admin.broadcastbestpracticw; import android.app.Activity; import java.util.ArrayList; import java.util.List; /** * Created by admin on 2016/3/18. */ public class ActivityCollector { 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(); } } } }
package com.example.admin.broadcastbestpracticw; import android.app.Activity; import android.os.Bundle; /** * Created by admin on 2016/3/18. */ public class BaseActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ActivityCollector.addActivity(this); } @Override protected void onDestroy() { super.onDestroy(); ActivityCollector.removeActivity(this); } }
package com.example.admin.broadcastbestpracticw; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; /** * Created by admin on 2016/3/18. */ public class LoginActivity extends BaseActivity{ private EditText accountEdit; private EditText passwordEdit; private Button login; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.login); accountEdit= (EditText) findViewById(R.id.account); passwordEdit= (EditText) findViewById(R.id.password); login= (Button) findViewById(R.id.login); login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String account=accountEdit.getText().toString(); String password=passwordEdit.getText().toString(); //如果账号是admin密码是123456 就认为登陆成功 if(account.equals("admin")&&password.equals("123456")){ Intent intent=new Intent(LoginActivity.this,MainActivity.class); startActivity(intent); finish(); }else{ Toast.makeText(LoginActivity.this,"account or password is invalid",Toast.LENGTH_SHORT).show();; } } }); } }第四个 主界面 设置
package com.example.admin.broadcastbestpracticw; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button forceOffline= (Button) findViewById(R.id.force_offline); forceOffline.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent("com.example.broadcastbewspractice.FORCE_OFFLINE"); sendBroadcast(intent); } }); } }
package com.example.admin.broadcastbestpracticw; import android.app.AlertDialog; import android.content.BroadcastReceiver; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.view.WindowManager; /** * Created by admin on 2016/3/18. */ public class ForceOfflineReceiver extends BroadcastReceiver { @Override public void onReceive(final Context context, final Intent intent) { //用AlertDialog来创建一个对话框 AlertDialog.Builder dialogBuilder=new AlertDialog.Builder(context); dialogBuilder.setTitle("Warning!!!!"); dialogBuilder.setMessage("你已被强制下线,请再次登录"); dialogBuilder.setCancelable(false); dialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { ActivityCollector.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(); } }