Android (5)

First, the configuration file in activity

    When we do not use orientation, create a new class. To make him a jump interface, you must register the newly created class in the configuration file and write it in the application tag of the AndroidManifest.XML configuration file

		<activity 
            android:name=".B"
            android:label="@string/app_name">//android:label="B">这个是一个标签,可以改名字
		 </activity>
2. Activity returns information
  1. activity returns confidence
    	//在A界面中--------------------------------
    	public void toB(View v){
    		Button btnlogin=(Button)findViewById(R.id.login);
    		EditText user=(EditText)findViewById(R.id.username);
    		String username=user.getText().toString();
    		Intent intent=new Intent();
    		intent.putExtra("name", username);
    		intent.setClass(MainActivity.this, B.class);
    		//startActivity(intent);
    		startActivityForResult(intent, 1);//注意这里,需要回传信息,就需要把上面的句子换成下面的句子,返回一个结果,参数为跳转页面和状态码
    	}
    	//需要在oncreate函数外面再写一个函数,如下
    	protected void onActivityResult(int requestCode,int resultCode,Intent data){//第一个参数为请求码(在上面写的时候可以自己设置),第二个参数为返回结果码值(该值在跳转到的界面里面result里面设置)
    		if(requestCode==1&&resultCode==2){
    			String result=data.getStringExtra("result");//返回页面设置的字符串,从这里获得
    			Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT).show();
    		}
    	}
    	//写在B界面中----------------------------------
    	public void backA(View v){//按钮点击事件
    		Intent intent=new Intent();
    		intent.putExtra("result", "你已登录");
    		setResult(2,intent);//设置返回页(注意:除了返回密码,后面还有一个界面跳转意图intent)
    		finish();//结束当前界面
    	}
    
  2. Simple setting text (simplified version)
    	if(requestCode==1&&resultCode==3){
    			TextView text=(TextView) findViewById(R.id.textView3);
    			text.setText("你已登录···············");
    	}	
    	public void backA(View v){ 
    		setResult(3);//也可以直接返回状态码,简单的像上面一样设置文本
    		finish();//结束当前界面
    	}
    
3. Broadcast (anti-loss function)
  1. Create a static broadcast
    		public void onClick(View arg0) {//点击事件创建
    			Intent intent=new Intent(MainActivity.this,BroadCastReceiver.class);//直接把跳转类的setclass()合并到这里
    			sendBroadcast(intent);//发送广播	
    		}
    	//接收广播需要新建一个Java文件,创建接收器基本结构
    		public class BroadCastReceiver extends BroadcastReceiver{//广播 接受器基本结构
    			public void onReceive(Context arg0, Intent arg1) {
    				Toast.makeText(arg0, "接收到广播了", Toast.LENGTH_SHORT).show();
    			}
    		}
    	//配置文件中配置新建的接收器文件(放在AndroidManifest.XML中的)
    		<receiver 
    			android:name="com.example.broad.BroadCastReceiver">
    		</receiver>
    
  2. Create a dynamic broadcast (need to create a new Java file as a broadcast receiver, no need to create a configuration file)
    2.1 This should be placed in the configuration file tab of the Java file of the broadcast receiver, but dynamic is not required, directly in the main interface Java file Write in Chinese, as follows 2.2
    		<intent-filter >
    			<action
    				android:name="android.net.conn.CONNECTIVITY_CHANGE">     //过滤条件,就是2.2中的过滤条件
    		</intent-filter>
    
    2.2 (written in onCreate function)
    		//动态创建广播(监测系统广播)
    		   BroadCastReceiver myBroadCast=new BroadCastReceiver();//声明一个广播对象
    		   IntentFilter it=new IntentFilter();//过滤器
    		   it.addAction("android.net.conn.CONNECTIVITY_CHANGE");//增加过滤条件 ,注册(相当于在配置文件中配置)
    		   registerReceiver(myBroadCast, it);//注册接收器(第一个参数:广播的名称,第二个参数:过滤的条件)
    		}
    
    2.3 Note ***
    		 //动态注册的广播,程序完全关闭时,尽量注销掉
    		protected void onDestroy() {
    			super.onDestroy();
    			unregisterReceiver(myBroadCast);//当程序完全关闭时,注销掉广播
    		}
    
    2.4 The difference between dynamic broadcasting and static broadcasting
    1> No configuration file is required
    2> The first one is that the broadcast file is there no matter if it is a software switch. In the second, the broadcast can be closed as the software is closed.
  3. Broadcast monitoring between two apps
    1> a_app
    3.1 a_app requires a broadcast receiver class MyBroadCastReceiver extends BroadcastReceiver
    3.2 The broadcast receiver needs to write the characteristics of specific monitoring software private String action = “com.example.broadcast.MY_self”; //com.example.broadcast.MY_self Write
    3.3 Add an if judgment in onReceive to match the characteristics
    			public void onReceive(Context context, Intent intent) {
    				if(action.equals(intent.getAction())){//action为上面设置设置的要监听的软件的特征
    					Toast.makeText(context, "嘿嘿被我逮到了吧", Toast.LENGTH_SHORT).show();
    				}
    			}
    
    3.4 Add tags in the configuration file of a_app (register broadcast receiver)
    			<receiver 
    				android:name="com.example.a_app.MyBroadCastReceiver">
    				<intent-filter >  //过滤器标签
    					<action android:name="com.example.broadcast.MY_self"/>   //在过滤器中加要监听的特征
    				</intent-filter>
    			</receiver>
    
    2> b_app
    1.b_app needs to add its own characteristics in the specific click event settings in the Java file of the main interface
    			Intent intent=new Intent();
    			intent.setAction("com.example.broadcast.MY_self");//设置自己特征
    			sendBroadcast(intent);//发送广播
    
4. Picture click prompt (batch use of switch is more convenient)
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.mar1:Toast.makeText(getApplicationContext(), "钢铁侠", 2).show();
			break;
		case R.id.mar2:Toast.makeText(getApplicationContext(), "mar2", 2).show();
			break;
		default:
			break;
		}
	}
Published 72 original articles · praised 3 · visits 3564

Guess you like

Origin blog.csdn.net/id__39/article/details/104775700