请选择 进入手机版 | 继续访问电脑版
查看: 2773|回复: 9

[原创] 【高校争霸赛】新型智能插座(5)——APP设计

[复制链接]
  • TA的每日心情
    郁闷
    2021-3-10 19:44
  • 签到天数: 7 天

    [LV.3]偶尔看看II

    126

    主题

    525

    帖子

    0

    金牌会员

    Rank: 6Rank: 6

    积分
    2018
    最后登录
    2023-12-25
    发表于 2016-11-29 14:26:33 | 显示全部楼层 |阅读模式
    本帖最后由 MDebug 于 2016-11-29 14:34 编辑

    APP主要用于对插座上各个继电器进行控制。此软件通过手机与WIFI模块建立连接,然后通过串口方式将数据发送到插座的MCU上,通过对数据的处理达到对插座相应控制。
    界面显示内容有:插座端WIFI模块IP地址,端口号(事先将此项设置好,使用的时候不用再改变)。端口号后面显示的为连接状态。ONOFF分别表示继电器的控制状态,即每个插孔的工作状态。
    2.jpg
    做工很粗糙,也没有优化。
    部分代码:
    Part1:
    1. <font face="新宋体"><?xml version="1.0" encoding="utf-8"?>
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3.               android:orientation="vertical"
    4.               android:layout_width="fill_parent"
    5.               android:layout_height="fill_parent"
    6.     <TextView>
    7.             android:layout_width="fill_parent"
    8.             android:layout_height="180dp"
    9.             android:text="@string/default_message"
    10.             android:id="@+id/hellotextView" android:textColor="#00ff00" android:gravity="center"/>
    11.     <Button>
    12.             android:layout_width="wrap_content"
    13.             android:layout_height="wrap_content"
    14.             android:text="@string/button_send"
    15.             android:id="@+id/hellobutton" android:layout_gravity="center"/>
    16. android:layout_width="wrap_content"
    17.             android:layout_height="wrap_content"
    18.             android:text="@string/button_send"
    19.             android:id="@+id/hellobutton" android:layout_gravity="center"/>
    20. </LinearLayout>
    21. <?xml version="1.0" encoding="utf-8"?>

    22. <resources>

    23.     <string name="app_name">helloandroid by hiwanz</string>

    24.     <string name="button_send">Say something</string>

    25.     <string name="default_message">Click button below!</string>

    26.     <string name="interact_message">You just clicked on the Button!</string>

    27. </resources>
    28. package com.aa.ta;

    29. import android.app.Activity;

    30. import android.os.Bundle;

    31. import android.view.View;

    32. import android.widget.Button;

    33. import android.widget.TextView;

    34. import android.widget.Toast;

    35. public class MyActivity extends Activity {

    36.     /**

    37.      * Called when the activity is first created.

    38.      */

    39.     @Override

    40.     public void onCreate(Bundle savedInstanceState)
    41. {

    42.         super.onCreate(savedInstanceState);

    43.         setContentView(R.layout.main);

    44.         Button hellobtn = (Button)findViewById(R.id.tnbtn);

    45.         tnbtn.setOnClickListener(new View.OnClickListener()
    46. {

    47.             @Override

    48.             public void onClick(View v)
    49. {

    50.                 TextView hellotv = (TextView)findViewById(R.id.hellotextView);

    51.                 Toast.makeText(MyActivity.this,"Clicked",Toast.LENGTH_SHORT).show();

    52.                 //读取strings.xml定义的interact_message信息并写到textview上

    53.                 hellotv.setText(R.string.interact_message);

    54.             }
    55.       tnbtn.setOffClickListener(new View.OffClickListener()
    56. {

    57.             @Override

    58.             public void OffClick(View X)
    59. {

    60.                 TextView hellotv = (TextView)findViewById(R.id.hellotextView);


    61.                 Toast.makeText(MyActivity.this,"Clicked",Toast.LENGTH_SHORT).show();

    62.                 //读取strings.xml定义的interact_message信息并写到textview上

    63.                 hellotv.setText(R.string.interact_message);

    64.             }

    65.         };

    66.     }

    复制代码
    Part2:
    1. <uses-permission

    2.   android:name= "android.permission.GET_TASKS" />

    3. //判断当前手机是否处于锁屏(睡眠)状态
    4. public static boolean isSleeping(Context context) {

    5.    KeyguardManager kgMgr = (KeyguardManager) context

    6.      .getSystemService(Context.KEYGUARD_SERVICE);

    7.    boolean isSleeping = kgMgr.inKeyguardRestrictedInputMode();

    8.    return isSleeping;

    9.   }
    10. public static boolean isOnline(Context context) {

    11.    ConnectivityManager manager = (ConnectivityManager) context

    12.      .getSystemService(Activity.CONNECTIVITY_SERVICE);

    13.    NetworkInfo info = manager.getActiveNetworkInfo();

    14.    if (info != null && info.isConnected()) {

    15.     return true ;

    16.    }

    17.    return false ;

    18.   }
    19. public static boolean isWifiConnected(Context context) {

    20.   ConnectivityManager connectivityManager = (ConnectivityManager) context

    21.     .getSystemService(Context.CONNECTIVITY_SERVICE);

    22.   NetworkInfo wifiNetworkInfo = connectivityManager

    23.     .getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    24.   if (wifiNetworkInfo.isConnected()) {

    25.    return true ;

    26.   }

    27.   return false ;

    28. }
    29. public static void installApk(Context context, File file) {

    30.   Intent intent = new Intent();

    31.   intent.setAction( "android.intent.action.VIEW" );

    32.   intent.addCategory( "android.intent.category.DEFAULT" );

    33.   intent.setType( "application/vnd.android.package-archive" );

    34.   intent.setDataAndType(Uri.fromFile(file),

    35.     "application/vnd.android.package-archive" );

    36.   intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    37.   context.startActivity(intent);

    38. }
    39. public static boolean isPhone(Context context) {

    40.   TelephonyManager telephony = (TelephonyManager) context

    41.     .getSystemService(Context.TELEPHONY_SERVICE);

    42.   if (telephony.getPhoneType() == TelephonyManager.PHONE_TYPE_NONE) {

    43.    return false ;

    44.   } else {

    45.    return true ;

    46.   }

    47. }@SuppressWarnings ( "deprecation" )

    48. public static int getDeviceWidth(Context context) {

    49.   WindowManager manager = (WindowManager) context

    50.     .getSystemService(Context.WINDOW_SERVICE);

    51.   return manager.getDefaultDisplay().getWidth();

    52. }

    53. @SuppressWarnings ( "deprecation" )

    54. public static int getDeviceHeight(Context context) {

    55.   WindowManager manager = (WindowManager) context

    56.     .getSystemService(Context.WINDOW_SERVICE);

    57.   return manager.getDefaultDisplay().getHeight();

    58. }

    59. @TargetApi (Build.VERSION_CODES.CUPCAKE)

    60. public static String getDeviceIMEI(Context context) {

    61.   String deviceId;

    62.   if (isPhone(context)) {

    63.    TelephonyManager telephony = (TelephonyManager) context

    64.      .getSystemService(Context.TELEPHONY_SERVICE);

    65.    deviceId = telephony.getDeviceId();

    66.   } else {

    67.    deviceId = Settings.Secure.getString(context.getContentResolver(),

    68.      Settings.Secure.ANDROID_ID);

    69.   }

    70.   return deviceId;

    71. }
    72. public static String getAppVersion(Context context) {

    73.   String version = "0" ;

    74.   try {

    75.    version = context.getPackageManager().getPackageInfo(

    76.      context.getPackageName(), 0 ).versionName;

    77.   } catch (PackageManager.NameNotFoundException e) {

    78.    e.printStackTrace();

    79.   }

    80.   return version;

    81. }
    82. public static Properties collectDeviceInfo(Context context) {

    83.    Properties mDeviceCrashInfo = new Properties();

    84.    try {

    85.     PackageManager pm = context.getPackageManager();

    86.     PackageInfo pi = pm.getPackageInfo(context.getPackageName(),

    87.       PackageManager.GET_ACTIVITIES);

    88.     if (pi != null ) {

    89.      mDeviceCrashInfo.put(VERSION_NAME,

    90.        pi.versionName == null ? "not set" : pi.versionName);

    91.      mDeviceCrashInfo.put(VERSION_CODE, pi.versionCode);

    92.     }

    93.    } catch (PackageManager.NameNotFoundException e) {

    94.     Log.e(TAG, "Error while collect package info" , e);

    95.    }

    96.    Field[] fields = Build. class .getDeclaredFields();

    97.    for (Field field : fields) {

    98.     try {

    99.      field.setAccessible( true );

    100.      mDeviceCrashInfo.put(field.getName(), field.get( null ));

    101.     } catch (Exception e) {

    102.      Log.e(TAG, "Error while collect crash info" , e);

    103.     }

    104.    }

    105.    return mDeviceCrashInfo;

    106.   }

    107. public static String collectDeviceInfoStr(Context context) {

    108.    Properties prop = collectDeviceInfo(context);

    109.    Set deviceInfos = prop.keySet();

    110.    StringBuilder deviceInfoStr = new StringBuilder( "{\n" );

    111.    for (Iterator iter = deviceInfos.iterator(); iter.hasNext();) {

    112.     Object item = iter.next();

    113.     deviceInfoStr.append( "\t\t\t" + item + ":" + prop.get(item)

    114.       + ", \n" );

    115.    }

    116.    deviceInfoStr.append( "}" );

    117.    return deviceInfoStr.toString();

    118.   }

    119. @TargetApi (Build.VERSION_CODES.CUPCAKE)

    120.   public static void hideSoftInput(Activity activity) {

    121.    View view = activity.getWindow().peekDecorView();

    122.    if (view != null ) {

    123.     InputMethodManager inputmanger = (InputMethodManager) activity

    124.       .getSystemService(Context.INPUT_METHOD_SERVICE);

    125.     inputmanger.hideSoftInputFromWindow(view.getWindowToken(), 0 );

    126.    }

    127.   }

    128.   @TargetApi (Build.VERSION_CODES.CUPCAKE)

    129. public static void hideSoftInput(Context context, EditText edit) {

    130.    edit.clearFocus();

    131.    InputMethodManager inputmanger = (InputMethodManager) context

    132.      .getSystemService(Context.INPUT_METHOD_SERVICE);

    133.    inputmanger.hideSoftInputFromWindow(edit.getWindowToken(), 0 );

    134.   }

    135. @TargetApi (Build.VERSION_CODES.CUPCAKE)

    136. public static void showSoftInput(Context context, EditText edit) {

    137.    edit.setFocusable( true );

    138.    edit.setFocusableInTouchMode( true );

    139.    edit.requestFocus();

    140.    InputMethodManager inputManager = (InputMethodManager) context

    141.      .getSystemService(Context.INPUT_METHOD_SERVICE);

    142.    inputManager.showSoftInput(edit, 0 );

    143.   }

    144. @TargetApi (Build.VERSION_CODES.CUPCAKE)

    145. public static void toggleSoftInput(Context context, EditText edit) {

    146.    edit.setFocusable( true );

    147.    edit.setFocusableInTouchMode( true );

    148.    edit.requestFocus();

    149.    InputMethodManager inputManager = (InputMethodManager) context

    150.      .getSystemService(Context.INPUT_METHOD_SERVICE);

    151.    inputManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0 );

    152.   }

    153. public static void goHome(Context context) {

    154.    Intent mHomeIntent = new Intent(Intent.ACTION_MAIN);

    155.    mHomeIntent.addCategory(Intent.CATEGORY_HOME);

    156.    mHomeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK

    157.      | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);

    158.    context.startActivity(mHomeIntent);

    159.   }
    160. @TargetApi (Build.VERSION_CODES.CUPCAKE)

    161. public static int getStatusBarHeight(Activity activity) {

    162.   Rect frame = new Rect();

    163.   activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);

    164.    return frame.top;

    165.   }
    166. public static int getTopBarHeight(Activity activity) {

    167.    return activity.getWindow().findViewById(Window.ID_ANDROID_CONTENT)

    168.      .getTop();

    169.   }
    复制代码



    很开心
    回复

    使用道具 举报

  • TA的每日心情
    开心
    2018-7-23 21:04
  • 签到天数: 103 天

    [LV.6]常住居民II

    228

    主题

    5379

    帖子

    0

    金牌会员

    Rank: 6Rank: 6

    积分
    15047
    最后登录
    1970-1-1
    发表于 2016-11-29 15:28:23 | 显示全部楼层
    很不错啦,支持一个
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2016-12-5 08:29
  • 签到天数: 5 天

    [LV.2]偶尔看看I

    0

    主题

    107

    帖子

    0

    中级会员

    Rank: 3Rank: 3

    积分
    338
    最后登录
    2018-1-4
    发表于 2016-11-29 16:53:56 | 显示全部楼层
    不错不错   ,很详细
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    昨天 08:55
  • 签到天数: 2602 天

    [LV.Master]伴坛终老

    45

    主题

    5158

    帖子

    22

    金牌会员

    Rank: 6Rank: 6

    积分
    10583
    最后登录
    2024-3-28
    发表于 2016-11-29 17:36:01 | 显示全部楼层
    谢谢分享,能用就好。
    签到签到
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2018-7-24 08:30
  • 签到天数: 50 天

    [LV.5]常住居民I

    22

    主题

    817

    帖子

    0

    金牌会员

    Rank: 6Rank: 6

    积分
    1713
    最后登录
    2019-12-7
    发表于 2016-11-29 21:16:39 | 显示全部楼层
    不错,支持下
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    无聊
    2017-1-24 08:47
  • 签到天数: 49 天

    [LV.5]常住居民I

    0

    主题

    524

    帖子

    0

    金牌会员

    Rank: 6Rank: 6

    积分
    1267
    最后登录
    2017-4-19
    发表于 2016-11-30 08:47:47 | 显示全部楼层
    厉害了                           
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    昨天 11:40
  • 签到天数: 801 天

    [LV.10]以坛为家III

    70

    主题

    2409

    帖子

    24

    金牌会员

    Rank: 6Rank: 6

    积分
    5384
    最后登录
    2024-3-28
    发表于 2016-12-1 09:59:57 | 显示全部楼层
    谢谢分享
    该会员没有填写今日想说内容.
    回复

    使用道具 举报

  • TA的每日心情
    开心
    2019-4-30 16:08
  • 签到天数: 184 天

    [LV.7]常住居民III

    3

    主题

    651

    帖子

    0

    金牌会员

    Rank: 6Rank: 6

    积分
    1310
    最后登录
    2020-11-30
    发表于 2016-12-3 15:34:30 | 显示全部楼层
    厉害了  0.....
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    擦汗
    2017-10-15 13:16
  • 签到天数: 191 天

    [LV.7]常住居民III

    11

    主题

    664

    帖子

    0

    金牌会员

    Rank: 6Rank: 6

    积分
    1722
    最后登录
    2017-10-15
    发表于 2016-12-3 16:59:54 | 显示全部楼层
    谢谢分享        有实物图更好
    该会员没有填写今日想说内容.
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    奋斗
    2022-1-8 18:28
  • 签到天数: 93 天

    [LV.6]常住居民II

    3

    主题

    221

    帖子

    0

    高级会员

    Rank: 4

    积分
    508
    最后登录
    2022-1-8
    发表于 2016-12-7 12:31:50 | 显示全部楼层
    谢谢分享,学习了
    哎...今天够累的,签到来了~
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /4 下一条

    Archiver|手机版|小黑屋|恩智浦技术社区

    GMT+8, 2024-3-29 03:55 , Processed in 0.132768 second(s), 30 queries , MemCache On.

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

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