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