首页 > 其他分享 >Android 学习笔记1

Android 学习笔记1

时间:2022-08-24 17:16:18浏览次数:64  
标签:笔记 public 学习 Intent id import Android android view

Android 学习笔记1

需求:1.按钮响应、文本更新

2.动态注册广播,实现接收系统分钟广播,跳转界面

3.在子线程中实现倒计时1分钟

4.将Activity与Service绑定、解绑,开关Service服务,实现监控

5.将APP发布在奇瑞主机端

MainActivity.java

package com.example.releasemain;

import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
import broadcast.Receiver;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private BroadcastReceiver Receiver;

    @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.view_button).setOnClickListener(this);
        findViewById(R.id.view_but_back).setOnClickListener(this);
    }

    //设置按键监听
    @Override
    public void onClick(View v) {

        if(v.getId()==R.id.view_button){
//            Toast.makeText(this,"测试")
            Intent intent=new Intent();
            intent.setClass(this,Page2.class);
            startActivities(new Intent[]{intent});

        }
        if(v.getId()==R.id.view_but_back) {
            finish();
        }

    }

    //注册与注销广播
    @Override
    protected void onStart() {
        super.onStart();
        Receiver = new Receiver(); // 创建一个分钟变更的广播接收器
        // 创建一个意图过滤器,只处理系统分钟变化的广播
        IntentFilter filter = new IntentFilter(Intent.ACTION_TIME_TICK);

        registerReceiver(Receiver, filter);}
    @Override
    protected void onStop() {
        super.onStop();
        unregisterReceiver(Receiver);}
}

page2.java

package com.example.releasemain;

import androidx.appcompat.app.AppCompatActivity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.os.Bundle;
import android.widget.TextView;
import service.MyService;


public class Page2 extends AppCompatActivity  implements View.OnClickListener{

//    绑定service
    public MyService.DownloadBinder downloadBinder;
    private final ServiceConnection connection=new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            downloadBinder=(MyService.DownloadBinder) service;
            downloadBinder.startDownload();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_page2);
        findViewById(R.id.view_but_back2).setOnClickListener(this);
        findViewById(R.id.view_but_back3).setOnClickListener(this);
        findViewById(R.id.view_but_back5).setOnClickListener(this);
        findViewById(R.id.view_but_back6).setOnClickListener(this);
        findViewById(R.id.view_but_back7).setOnClickListener(this);
        findViewById(R.id.view_but_back8).setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        TextView view_page2_text=findViewById(R.id.view_page2_text);
        view_page2_text.setText(R.string.twoValue);
        final Intent intent=new Intent();
        intent.setClass(this, MyService.class);
        if(v.getId()==R.id.view_but_back2) {
            Log.d("Page2", "onClick:点击了返回 ");
            finish();
        } else if (v.getId()==R.id.view_but_back3) {
//            开启线程
            Log.d("Page2", "onClick:进入了子线程 ");
            System.out.println("进入子线程");
            new Thread(){
                TextView view_page2_text=findViewById(R.id.view_page2_text);
                private long data;
                @Override
                public void run() {
                    super.run();
                    data=60000;
                    int cx=1000;
                    for(int i=60;i>0;i--){
                        System.out.println(i);
                        view_page2_text.setText("计时:"+(i-1)+"秒");
                        SystemClock.sleep(cx);
                    }
                    view_page2_text.setText(R.string.button4);
                }
            }.start();
            }
//        启动与停止service服务
        else if (v.getId()==R.id.view_but_back5){
            startService(intent);
        }else if (v.getId()==R.id.view_but_back6){
            stopService(intent);
        }
        else if (v.getId()==R.id.view_but_back7){
//            启动,绑定service
//            startService(intent);
            bindService(intent,connection,BIND_AUTO_CREATE);
        }else if (v.getId()==R.id.view_but_back8){
            unbindService(connection);
        }

    }}

Receiver.java

package broadcast;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
import com.example.releasemain.MainActivity;
import com.example.releasemain.Page2;

public class Receiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.
        Log.i("Receiver", "onReceive: oik");
        System.out.println(intent);
        if((intent!=null)){
            Toast.makeText(context,"Broadcast Complete", Toast.LENGTH_LONG).show();

            System.out.println("收到分钟广播");
            Log.i("Receiver", "onReceive: ok");
            Intent start=new Intent(context, Page2.class);
            context.startActivities(new Intent[]{start});
            };
        }
}

MyService.java

package service;
package service;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class MyService extends Service {
    public MyService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d("MyService", "启动Service_onCreate");
        Toast.makeText(this, "启动Service_onCreate", Toast.LENGTH_LONG).show();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d("MyService", "启动Service_onStartCommand");
        Toast.makeText(this, "启动Service_onStartCommand", Toast.LENGTH_LONG).show();
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d("MyService", "启动Service_onDestroy");
        Toast.makeText(this, "启动Service_onDestroy", Toast.LENGTH_LONG).show();
    }

//    绑定service

    public DownloadBinder mBinder = new DownloadBinder();

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    public class DownloadBinder extends Binder {
        public void startDownload() {
            Log.d("MyService", "startDownload: downloadBinder");
            System.out.println("进入service-DownloadBinder-startDownload中");

        }
    }

}

Androidmanifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.ReleaseMain"
        tools:targetApi="31">
        <service
            android:name="service.MyService"
            android:enabled="true"
            android:exported="true">

        </service>

        <receiver
            android:name="broadcast.Receiver"
            android:enabled="true"
            android:exported="true"></receiver>

        <activity
            android:name=".Page2"
            android:exported="false">
            <meta-data
                android:name="android.app.lib_name"
                android:value="" />
        </activity>
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <meta-data
                android:name="android.app.lib_name"
                android:value="" />
        </activity>
    </application>

</manifest>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/view_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="@string/beginValue" />

        <Button
            android:id="@+id/view_button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/button1" />

        <Button
            android:id="@+id/view_but_back"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/button2" />

    </LinearLayout>

</LinearLayout>

activity_page2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    tools:context=".Page2">

    <LinearLayout

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="280dp"
            android:src="@drawable/backbone" />

        <TextView
            android:id="@+id/view_page2_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="@string/twoValue"
            android:textColor="@color/black" />

        <Button
            android:id="@+id/view_but_back3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/button3" />

        <Button
            android:id="@+id/view_but_back5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/button5" />

        <Button
            android:id="@+id/view_but_back6"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/button6" />

        <Button
            android:id="@+id/view_but_back7"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/button7" />

        <Button
            android:id="@+id/view_but_back8"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/button8" />

        <Button
            android:id="@+id/view_but_back2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/button2" />

    </LinearLayout>

</LinearLayout>

strings.xml

<resources>
    <string name="app_name">ReleaseMain</string>
    <string name="beginValue">文本:原来界面</string>
    <string name="twoValue">文本:跳转后界面</string>
    <string name="button1">我是跳转按钮</string>
    <string name="button2">我是返回按钮</string>
    <string name="button3">开始倒计时</string>
    <string name="button4">计时1分钟结束</string>
    <string name="button5">启动Service</string>
    <string name="button6">停止Service</string>
    <string name="button7">绑定Service</string>
    <string name="button8">解绑Service</string>
</resources>

标签:笔记,public,学习,Intent,id,import,Android,android,view
From: https://www.cnblogs.com/ipythonyang/p/16620800.html

相关文章

  • [ROS学习]4.ROS命令行工具
    笔记参考:【ROS学习笔记】4.玩转小海龟——ROS命令行工具基于B站ROS公开课:【古月居】古月·ROS入门21讲基于Ubuntu20.04.1、Noetic版本1回顾第一个小海龟程序打开小海......
  • Ladon代码学习
    代码地址https://github.com/k8gege/LadonGo按照Ladon的readme功能介绍来学习win环境需要先安装gcc https://blog.csdn.net/yvge669/article/details/124564622一,信息......
  • FastApi学习
    vscode配置插件coderunner在setting.json中关于python的修改为,因为我使用了虚拟环境,得让vscode找到python的路径"code-runner.executorMap":{"python":"......
  • 论文阅读笔记-3D-LaneNet: End-to-End 3D Multiple Lane Detection
    3D-LaneNet:End-to-End3DMultipleLaneDetection3D-LaneNet:端到端3D多车道检测Abstract我们引入了一个网络,可以直接从单个图像预测道路场景中车道的3D布局。这......
  • Python小白自学笔记:英语不好,变量怎么命名
    变量其实很简单,不过在使用过程中会遇到一些棘手的问题。比如一个变量我之前已经用过了,现在我要定义一个类似的变量,该怎么办?还有,很多小伙伴其实知道变量应该遵守什么规范,......
  • 「学习笔记」不动点法求数列通项
    前言不动点法求数列通项是怎么回事呢?不动点法相信大家都很熟悉,但是不动点法求数列通项是怎么回事呢,下面就让小编带大家一起了解吧不动点法求数列通项,其实就是数列通项可......
  • 直播电商平台开发,android cardview 取消阴影,高度
    直播电商平台开发,androidcardview取消阴影,高度 <androidx.cardview.widget.CardView      app:cardBackgroundColor="@color/white"      an......
  • Unity 笔记UnityXR简单使用
    插件导入:打开PackageManager添加XRInteractionToolki添加XRPluginManagement5.PS:如果PackgeManager找不到上面的插件,可以按照下图更改筛选条件。(感谢小pp侠提出意见)......
  • 04.Javascript学习笔记3
    1.箭头函数箭头函数是一种更短的函数表达式。constage=birthyear=>2022-birthyear;console.log(age(2000))箭头左边的birthyear是参数,箭头右边是要执行的代码......
  • 机器学习1
    常见的几种假设检验的实例以及对应python代码实现(包括基于图的效果展示Z检验t检验χ2检验F检验熟悉scikit-learn及其相关应用NumpyNumpy优势1.定义开源的pytho......