鱼C论坛

 找回密码
 立即注册
查看: 2083|回复: 0

[学习笔记] android programing 5.5

[复制链接]
发表于 2017-10-11 10:23:52 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
今天这个项目很简单,先新建个项目,在mainactivity.java旁边新建ActivityCollector.java
  1. package com.example.xinwei.broadcastbestpractice;

  2. import android.app.Activity;

  3. import java.util.ArrayList;
  4. import java.util.List;

  5. /**
  6. * Created by xinwei on 2017/10/11.
  7. */

  8. public class ActivityCollector {
  9.     public static List<Activity> activitys=new ArrayList<>();
  10.     public static void addActivity(Activity activity){
  11.         activitys.add(activity);
  12.     }
  13.     public static void removeActivity(Activity activity){
  14.         activitys.remove(activity);
  15.     }
  16.     public static void finishAll(){
  17.         for (Activity activity:activitys
  18.              ) {
  19.             if(!activity.isFinishing()){
  20.                 activity.finish();
  21.             }
  22.         }
  23.     }
  24. }
复制代码

然后新建BaseActivity.java
  1. package com.example.xinwei.broadcastbestpractice;

  2. import android.content.BroadcastReceiver;
  3. import android.content.Context;
  4. import android.content.DialogInterface;
  5. import android.content.Intent;
  6. import android.content.IntentFilter;
  7. import android.os.Bundle;
  8. import android.support.annotation.Nullable;
  9. import android.support.v7.app.AlertDialog;
  10. import android.support.v7.app.AppCompatActivity;
  11. import android.util.Log;

  12. /**
  13. * Created by xinwei on 2017/10/11.
  14. */

  15. public class BaseActivity extends AppCompatActivity {
  16.     private ForceOffLineReceiver receiver;

  17.     @Override
  18.     protected void onResume() {
  19.         super.onResume();
  20.         IntentFilter intentFilter=new IntentFilter();
  21.         intentFilter.addAction("alltolove.FORCE_OFFLINE");
  22.         receiver=new ForceOffLineReceiver();
  23.         registerReceiver(receiver,intentFilter);
  24.     }

  25.     @Override
  26.     protected void onPause() {
  27.         super.onPause();
  28.         if(receiver!=null){
  29.             unregisterReceiver(receiver);
  30.             receiver=null;
  31.         }
  32.     }

  33.     @Override
  34.     protected void onCreate(@Nullable Bundle savedInstanceState) {
  35.         super.onCreate(savedInstanceState);
  36.         ActivityCollector.addActivity(this);
  37.     }

  38.     @Override
  39.     protected void onDestroy() {
  40.         super.onDestroy();
  41.         ActivityCollector.removeActivity(this);
  42.     }

  43. }
复制代码

再建个ForceOffLineReceiver.java文件
  1. package com.example.xinwei.broadcastbestpractice;

  2. import android.content.BroadcastReceiver;
  3. import android.content.Context;
  4. import android.content.DialogInterface;
  5. import android.content.Intent;
  6. import android.support.v7.app.AlertDialog;
  7. import android.util.Log;

  8. public class ForceOffLineReceiver extends BroadcastReceiver {

  9.     @Override
  10.     public void onReceive(final Context context, Intent intent) {
  11.         Log.i("a","send");
  12.         AlertDialog.Builder builder=new AlertDialog.Builder(context);
  13.         builder.setTitle("Warning");
  14.         builder.setMessage("You are force to be offline,Please try to login again");
  15.         builder.setCancelable(false);
  16.         builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
  17.             @Override
  18.             public void onClick(DialogInterface dialogInterface, int i) {
  19.                 ActivityCollector.finishAll();
  20.                 Intent intent = new Intent(context,LoginActivity.class);
  21.                 context.startActivity(intent);
  22.             }
  23.         });
  24.         builder.show();
  25.     }
  26. }
复制代码

然后新建LoginActivity.java这个Activity(建活动空项目),修改系统自动生成的activity_login.xml文件
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:orientation="vertical">
  6.     <LinearLayout
  7.         android:orientation="horizontal"
  8.         android:layout_width="wrap_content"
  9.         android:layout_height="60dp">
  10.         <TextView
  11.             android:layout_width="90dp"
  12.             android:layout_height="wrap_content"
  13.             android:layout_gravity="center_vertical"
  14.             android:textSize="18sp"
  15.             android:text="Account:"/>
  16.         <EditText
  17.             android:id="@+id/account"
  18.             android:layout_width="0dp"
  19.             android:layout_height="wrap_content"
  20.             android:layout_weight="1"
  21.             android:layout_gravity="center_vertical"/>
  22.     </LinearLayout>
  23.     <LinearLayout
  24.         android:orientation="horizontal"
  25.         android:layout_width="wrap_content"
  26.         android:layout_height="60dp">
  27.         <TextView
  28.             android:layout_width="90dp"
  29.             android:layout_height="wrap_content"
  30.             android:layout_gravity="center_vertical"
  31.             android:textSize="18sp"
  32.             android:text="Password:"/>
  33.         <EditText
  34.             android:id="@+id/password"
  35.             android:layout_width="90dp"
  36.             android:layout_height="wrap_content"
  37.             android:layout_gravity="center_vertical"
  38.             android:inputType="textPassword"/>
  39.     </LinearLayout>
  40.     <Button
  41.         android:id="@+id/login"
  42.         android:layout_width="match_parent"
  43.         android:layout_height="60dp"
  44.         android:text="Login"/>
  45. </LinearLayout>
复制代码

接下来修改LoginActivity.java的代码
  1. package com.example.xinwei.broadcastbestpractice;

  2. import android.content.Intent;
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.Button;
  7. import android.widget.EditText;
  8. import android.widget.Toast;

  9. public class LoginActivity extends BaseActivity {
  10.     private EditText accountEdit;
  11.     private EditText passwordEdit;
  12.     private Button login;
  13.     @Override
  14.     protected void onCreate(Bundle savedInstanceState) {
  15.         super.onCreate(savedInstanceState);
  16.         setContentView(R.layout.activity_login);
  17.         accountEdit=(EditText)findViewById(R.id.account);
  18.         passwordEdit=(EditText)findViewById(R.id.password);
  19.         login = (Button)findViewById(R.id.login);
  20.         login.setOnClickListener(new View.OnClickListener() {
  21.             @Override
  22.             public void onClick(View view) {
  23.                 String account=accountEdit.getText().toString();
  24.                 String password=passwordEdit.getText().toString();
  25.                 if(account.equals("admin")&&password.equals("123456")){
  26.                     Intent intent=new Intent(LoginActivity.this,MainActivity.class);
  27.                     startActivity(intent);
  28.                     finish();
  29.                 }else{
  30.                     Toast.makeText(LoginActivity.this, "account or password is invalid", Toast.LENGTH_SHORT).show();
  31.                 }
  32.             }
  33.         });
  34.     }
  35. }
复制代码
然后修改AndroidManifest.xml文件,目的是把一开始启动的活动更改成LoginActivity,以下为代码
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3.     package="com.example.xinwei.broadcastbestpractice">

  4.     <application
  5.         android:allowBackup="true"
  6.         android:icon="@mipmap/ic_launcher"
  7.         android:label="@string/app_name"
  8.         android:roundIcon="@mipmap/ic_launcher_round"
  9.         android:supportsRtl="true"
  10.         android:theme="@style/AppTheme">
  11.         <activity android:name=".MainActivity">
  12.             
  13.         </activity>
  14.         <activity android:name=".LoginActivity">
  15.             <intent-filter>
  16.                 <action android:name="android.intent.action.MAIN" />

  17.                 <category android:name="android.intent.category.LAUNCHER" />
  18.             </intent-filter>
  19.         </activity>
  20.     </application>

  21. </manifest>
复制代码
注意:里面的包名不要照抄

然后修改activity_main.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"
  4.     xmlns:tools="http://schemas.android.com/tools"
  5.     android:layout_width="match_parent"
  6.     android:layout_height="match_parent"
  7.     tools:context="com.example.xinwei.broadcastbestpractice.MainActivity">

  8.     <Button
  9.         android:id="@+id/force_offline"
  10.         android:layout_width="match_parent"
  11.         android:layout_height="wrap_content"
  12.         android:text="sed force offline broadcast"/>

  13. </android.support.constraint.ConstraintLayout>
复制代码

修改mainactivity.java
  1. package com.example.xinwei.broadcastbestpractice;

  2. import android.content.Intent;
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.Button;

  7. public class MainActivity extends BaseActivity {

  8.     @Override
  9.     protected void onCreate(Bundle savedInstanceState) {
  10.         super.onCreate(savedInstanceState);
  11.         setContentView(R.layout.activity_main);
  12.         Button forceOffline=(Button)findViewById(R.id.force_offline);
  13.         forceOffline.setOnClickListener(new View.OnClickListener() {
  14.             @Override
  15.             public void onClick(View view) {
  16.                 Intent intent = new Intent("alltolove.FORCE_OFFLINE");
  17.                 sendBroadcast(intent);
  18.             }
  19.         });
  20.     }
  21. }
复制代码

效果图为:
jdfw.gif

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 09:56

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表