屏幕适配+广播

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <TextView
            android:layout_width="@dimen/x160"
            android:layout_height="wrap_content"
            android:text="姓名"
            android:background="#00ff00"
            />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />
    </LinearLayout>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="保存"
        android:layout_gravity="center_horizontal"
        android:background="@drawable/img1"
        />
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/img2"
        />

</LinearLayout>

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
        <TextView
            android:layout_width="@dimen/x160"
            android:layout_height="wrap_content"
            android:text="姓名"
            android:background="#00ff00"
            />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="保存"
        android:layout_gravity="center_horizontal"
        android:background="@drawable/img1"
        />
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/img2"
        />

</LinearLayout>

 

import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.PrintWriter; 
 
public class MakeXml { 
 
    private final static String rootPath = "D:\\layoutroot\\values-{0}x{1}\\"; 
 
    private final static float dw = 320f; 
    private final static float dh = 480f; 
 
    private final static String WTemplate = "<dimen name=\"x{0}\">{1}px</dimen>\n"; 
    private final static String HTemplate = "<dimen name=\"y{0}\">{1}px</dimen>\n"; 
 
    public static void main(String[] args) { 
        makeString(240, 320); 
        makeString(320, 480); 
        makeString(480,800); 
        makeString(480, 854);  
        makeString(600, 1024); 
        makeString(720, 1184); 
        makeString(720, 1196); 
        makeString(720, 1280); 
        makeString(768, 1024); 
        makeString(800, 1280); 
        makeString(1080, 1812); 
        makeString(1080, 1920); 
        makeString(1440, 2560); 
    } 
 
    public static void makeString(int w, int h) { 
 
        StringBuffer sb = new StringBuffer(); 
        sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"); 
        sb.append("<resources>"); 
        float cellw = w / dw; 
        for (int i = 1; i < 320; i++) { 
            sb.append(WTemplate.replace("{0}", i + "").replace("{1}", 
                    change(cellw * i) + "")); 
        } 
        sb.append(WTemplate.replace("{0}", "320").replace("{1}", w + "")); 
        sb.append("</resources>"); 
 
        StringBuffer sb2 = new StringBuffer(); 
        sb2.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"); 
        sb2.append("<resources>"); 
        float cellh = h / dh; 
        for (int i = 1; i < 480; i++) { 
            sb2.append(HTemplate.replace("{0}", i + "").replace("{1}", 
                    change(cellh * i) + "")); 
        } 
        sb2.append(HTemplate.replace("{0}", "480").replace("{1}", h + "")); 
        sb2.append("</resources>"); 
 
        String path = rootPath.replace("{0}", h + "").replace("{1}", w + ""); 
        File rootFile = new File(path); 
        if (!rootFile.exists()) { 
            rootFile.mkdirs(); 
        } 
        File layxFile = new File(path + "lay_x.xml"); 
        File layyFile = new File(path + "lay_y.xml"); 
        try { 
            PrintWriter pw = new PrintWriter(new FileOutputStream(layxFile)); 
            pw.print(sb.toString()); 
            pw.close(); 
            pw = new PrintWriter(new FileOutputStream(layyFile)); 
            pw.print(sb2.toString()); 
            pw.close(); 
        } catch (FileNotFoundException e) { 
            e.printStackTrace(); 
        } 
 
    } 
 
    public static float change(float a) { 
        int temp = (int) (a * 100); 
        return temp / 100f; 
    } 
}

广播

 

package com.example.text16;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;


//应用安装卸载的广播接收者
public class AppInstallReceiver extends BroadcastReceiver{

 @Override
 public void onReceive(Context context, Intent intent) {
  String action = intent.getAction();
  //获取的包名
  Uri data = intent.getData();
  if ("android.intent.action.PACKAGE_ADDED".equals(action)) {
   Log.e("TAG", "install-->"+data);
  } else if ("android.intent.action.PACKAGE_REMOVED".equals(action)) {
   Log.e("TAG", "removed-->"+data);
  }{

  }
 }

}

 

 

package com.example.text16;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

//开机启动的广播接收者
public class BootReciver extends BroadcastReceiver{

 @Override
 public void onReceive(Context context, Intent intent) {
  Log.e("TAG", "开机了");
  Intent i =  new Intent(context,MainActivity.class);
  /***
   * 现在是在广播接收着中创建activity
   * 当前没用任何activity在运行,所以不存在的一个任务栈
   * 需要制定义flag,在创建activity的同时创建任务栈
   */
  i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
  context.startActivity(i);
 }
 

}

 

package com.example.text16;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

//ip拨号器的广播接收者
public class DialReceiver extends BroadcastReceiver{

 @Override
 public void onReceive(Context context, Intent intent) {
  // 获取电话
  String number = getResultData();
  //
//  Log.e("Tag", "打电话"+number);
  //设置拨打的电话号 
  setResultData(number);
 }

}

 

 

package com.example.text16;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

//监听SD卡挂在状态的广播接收者
public class SDCarStateReceiver extends BroadcastReceiver {

 @Override
 public void onReceive(Context context, Intent intent) {
  // TODO Auto-generated method stub
  
  String action = intent.getAction();
  if ("android.intent.action.MEDIA_MOUNTED".equals(action)) {
   Log.e("TAG", "安装");
  } else if("android.intent.action.MEDIA_UNMOUNTED".equals(action)){
   Log.e("TAG", "缺载");

  }
  
 }

}

 

package com.example.text16;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsMessage;
import android.telephony.gsm.SmsManager;
import android.util.Log;

//短信广播接收者
public class SMSReceiver extends BroadcastReceiver{

 @Override
 public void onReceive(Context context, Intent intent) {
  
  Object[] object = (Object[])intent.getExtras().get("pdus");
  for (Object obj : object) {
   //创建短信的消息对象
   SmsMessage message = SmsMessage.createFromPdu((byte[])obj);
   //获取短信的发送者
   String from = message.getOriginatingAddress();
   //获取短信的内容
   String body = message.getMessageBody();
   Log.e("TAG", "from:" + from + "body"+ body);
  }
 }

}

 

posted @ 2018-05-24 19:33  8026  阅读(126)  评论(0编辑  收藏  举报