Android數據流
A. 為何Android手機在打開數據連接時會有數據流量產生
打開數據連接確實會有流量產生,因為打開數據連接時跟
網路伺服器
是有交互的。
另外手機中的
天氣插件
、新聞插件及
應用商店
等軟體的升級檢測,都會產生一定的數據流量,所以要建議用戶訂制套餐包。
B. android 獲取應用圖標bitmap跟data數據流怎麼有效
通過BitmapFactory這個工具類,BitmapFactory的所有函數都是static,這個輔助類可以通過資源ID、路徑、文件、數據流等方式來獲取點陣圖。大家可以打開API 看一下里邊全是靜態方法。這個類里邊有一個叫做 decodeStream(InputStream is)
此方法可以 解碼一個新的點陣圖從一個InputStream。這是獲得資源的InputStream。
代碼:
java代碼
InputStream is = getResources().openRawResource(R.drawable.icon);
Bitmap mBitmap = BitmapFactory.decodeStream(is);
Paint mPaint = new Paint();
canvas.drawBitmap(mBitmap, 40, 40, mPaint);
C. 如何用Android Service通過aidl傳遞一個數據流
第一步:部署我們的服務端,也就是Service端:
1:在Service端我先自定義2個類型:Person和Pet。因為我們需要跨進程傳遞Person對象和Pet對象,所以Person類和Pet類都必須實現Parcelable介面,並要求在實現類中定義一個名為CREATER,類型為Parcelable.creator的靜態Field。
代碼如下:
這是我Service端的部署情況(其中MainActivity可以不用去實現,因為我們只提供服務,沒有窗口顯示):
第二步:部署客戶端:
1.在客戶端新建一個包,命名需要和服務端放置aidl文件的包名相同(我這里是com.example.remoteservice),然後把服務端的Person.java,Pet.java,Person.aidl,Pet.aidl,IPet.aidl復制到這個包下面
2.在activity中綁定遠程服務進行數據交換,layout布局和activity代碼如下:
1 <RELATIVELAYOUT xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android" tools:context="com.example.remoteclient.RemoteClient" android:paddingtop="@dimen/activity_vertical_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingbottom="@dimen/activity_vertical_margin" android:layout_width="match_parent" android:layout_height="match_parent" 9="" 8="" 7="" 6="" 5="" 4="" 3="" 2="">
10
11 <LINEARLAYOUT android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" 14="" 13="" 12="">
15
16 <LINEARLAYOUT android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" 19="" 18="" 17="">
20
21 <EDITTEXT android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom" android:id="@+id/editText_person" android:ems="10" 26="" 25="" 24="" 23="" 22="">
27 </EDITTEXT>
28
29<BUTTON type=submit android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom" android:id="@+id/button_ok" android:text="確定" 34="" 33="" 32="" 31="" 30="">
35
36
37 <LISTVIEW android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/listView_pet" 40="" 39="" 38="">
41 </LISTVIEW>
42
43
44</BUTTON></LINEARLAYOUT></LINEARLAYOUT></RELATIVELAYOUT>
1 package com.example.remoteclient;
2
3 import android.app.Service;
4 import android.content.ComponentName;
5 import android.content.Intent;
6 import android.content.ServiceConnection;
7 import android.os.Bundle;
8 import android.os.IBinder;
9 import android.os.RemoteException;
10 import android.support.v7.app.ActionBarActivity;
11 import android.util.Log;
12 import android.view.View;
13 import android.view.View.OnClickListener;
14 import android.widget.ArrayAdapter;
15 import android.widget.Button;
16 import android.widget.EditText;
17 import android.widget.ListView;
18
19 import com.example.remoteservice.IPet;
20 import com.example.remoteservice.Person;
21 import com.example.remoteservice.Pet;
22
23 import java.util.List;
24
25 public class RemoteClient extends ActionBarActivity {
26
27 public static final String REMOTE_SERVICE_ACTION = com.example.remoteservice.RemoteService.ACTION;
28 EditText editText;
29 Button button;
30 ListView listView;
31
32 IPet petService;// 聲明IPet介面
33 List<PET> pets;
34 ServiceConnection conn = new ServiceConnection() {
35
36 @Override
37 public void onServiceDisconnected(ComponentName name) {
38 Log.i(csx, onServiceDisconnected);
39 conn = null;
40 }
41
42 @Override
43 public void onServiceConnected(ComponentName name, IBinder service) {
44 Log.i(csx, onServiceConnected);
45 petService = IPet.Stub.asInterface(service);// 通過遠程服務的Binder實現介面
46
47 }
48 };
49
50 @Override
51 protected void onCreate(Bundle savedInstanceState) {
52 super.onCreate(savedInstanceState);
53 setContentView(R.layout.remote_client_layout);
54 editText = (EditText) findViewById(R.id.editText_person);
55 button = (Button) findViewById(R.id.button_ok);
56 listView = (ListView) findViewById(R.id.listView_pet);
57
58 Intent service = new Intent();
59 service.setAction(REMOTE_SERVICE_ACTION);
60
61 bindService(service, conn, Service.BIND_AUTO_CREATE);// 綁定遠程服務
62
63 button.setOnClickListener(new OnClickListener() {
64
65 @Override
66 public void onClick(View v) {
67 String personName = editText.getText().toString();
68 if (personName == null || personName.equals()) {
69
70 return;
71 }
72
73 try {
74 pets = petService.getPets(new Person(1, personName, personName));// 調用遠程service的getPets方法
75 updataListView();
76
77 } catch (RemoteException e) {
78
79 e.printStackTrace();
80 } catch (NullPointerException e) {
81 e.printStackTrace();
82 }
83
84 }
85 });
86
87 }
88
89 public void updataListView() {
90 listView.setAdapter(null);
91
92 if (pets == null || pets.isEmpty()) {
93 return;
94
95 }
96 ArrayAdapter<PET> adapter = new ArrayAdapter<PET>(RemoteClient.this,
97 android.R.layout.simple_list_item_1, pets);
98 listView.setAdapter(adapter);
99
100 }
101
102 @Override
103 protected void onDestroy() {
104
105 unbindService(conn);// 解除綁定
106 super.onDestroy();
107 }
108
109 }</PET></PET></PET>
到此為止所有的工作都完成了,下面我們看一下效果:我在編輯框中輸入「csx」,點擊確定,就會顯示出服務端RemoteService中pets的相應數據。
D. android:怎樣將Uri類型的圖片數據轉換成流
ContentResolver resolver = getContentResolver();
Cursor cursor = resolver.query(originalUri, proj, null, null, null);
// 按我個人理解 這個是獲得用戶選擇的圖片的索引值
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
// 將游標移至開頭 ,這個很重要,不小心很容易引起越界
cursor.moveToFirst();
// 最後根據索引值獲取圖片路徑
String path = cursor.getString(column_index);
這樣就獲得了圖片的路徑。
下面說圖片上傳,現在一般上傳都用Okhttp 框架了,直接上傳個File類就可以,不需要自己在轉成數據流,給你個連接,我寫的工具類,你也可以查一下,這個很方便http://blog.csdn.net/xihe9152/article/details/68485040,使用前需要先依賴Okhttp3
E. android 操作系統使用數據流量指的是什麼有什麼辦法控制流量
root之後下載一個手機管家,裡面可以限制流量,一般不ROOT的話是不能限制流量的,操作系統使用數據流量指的是安卓系統聯網使用的流量,安卓系統有很多東西會後台聯網的,比如自帶的天氣軟體、谷歌商店等等東西,有時候系統本身也會聯網。
F. 將android怎麼把數據流打到buffer上
在android中的文件放在不同位置,它們的讀取方式也有一些不同。
本文對android中對資源文件的讀取、數據區文件的讀取、SD卡文件的讀取及RandomAccessFile的方式和方法進行了整理。供參考。
一、資源文件的讀取:apk中資源文件
1) 從resource的raw中讀取文件數據:
try{
//得到資源中的Raw數據流
InputStream in = getResources().openRawResource(R.raw.test);
//得到數據的大小
int length = in.available();
byte [] buffer = new byte[length];
//讀取數據
in.read(buffer);
//依test.txt的編碼類型選擇合適的編碼,如果不調整會亂碼
res = EncodingUtils.getString(buffer, "BIG5");
//關閉
in.close();
}catch(Exception e){
e.printStackTrace();
}
2) 從resource的asset中讀取文件數據
String fileName = "test.txt"; //文件名字
String res="";
try{
//得到資源中的asset數據流
InputStream in = getResources().getAssets().open(fileName);
int length = in.available();
byte [] buffer = new byte[length];
in.read(buffer);
in.close();
res = EncodingUtils.getString(buffer, "UTF-8");
}catch(Exception e){
e.printStackTrace();
}
二、讀寫/data/data/<應用程序名>目錄下的文件:
//寫數據
public void writeFile(String fileName,String writestr) throws IOException{
try{
FileOutputStream fout =openFileOutput(fileName, MODE_PRIVATE);
byte [] bytes = writestr.getBytes();
fout.write(bytes);
fout.close();
}
catch(Exception e){
e.printStackTrace();
}
}
//讀數據
public String readFile(String fileName) throws IOException{
String res="";
try{
FileInputStream fin = openFileInput(fileName);
int length = fin.available();
byte [] buffer = new byte[length];
fin.read(buffer);
res = EncodingUtils.getString(buffer, "UTF-8");
fin.close();
}
catch(Exception e){
e.printStackTrace();
}
return res;
}
三、讀寫SD卡中的文件。也就是/mnt/sdcard/目錄下面的文件 :
//寫數據到SD中的文件
public void writeFileSdcardFile(String fileName,String write_str) throws IOException{
try{
FileOutputStream fout = new FileOutputStream(fileName);
byte [] bytes = write_str.getBytes();
fout.write(bytes);
fout.close();
}
catch(Exception e){
e.printStackTrace();
}
}
//讀SD中的文件
public String readFileSdcardFile(String fileName) throws IOException{
String res="";
try{
FileInputStream fin = new FileInputStream(fileName);
int length = fin.available();
byte [] buffer = new byte[length];
fin.read(buffer);
res = EncodingUtils.getString(buffer, "UTF-8");
fin.close();
}
catch(Exception e){
e.printStackTrace();
}
return res;
}
四、使用File類進行文件的讀寫:
//讀文件
public String readSDFile(String fileName) throws IOException {
File file = new File(fileName);
FileInputStream fis = new FileInputStream(file);
int length = fis.available();
byte [] buffer = new byte[length];
fis.read(buffer);
res = EncodingUtils.getString(buffer, "UTF-8");
fis.close();
return res;
}
//寫文件
public void writeSDFile(String fileName, String write_str) throws IOException{
File file = new File(fileName);
FileOutputStream fos = new FileOutputStream(file);
byte [] bytes = write_str.getBytes();
fos.write(bytes);
fos.close();
}
五、另外,File類還有下面一些常用的操作:
String Name = File.getName(); //獲得文件或文件夾的名稱:
String parentPath = File.getParent(); //獲得文件或文件夾的父目錄
String path = File.getAbsoultePath();//絕對路經
String path = File.getPath();//相對路經
File.createNewFile();//建立文件
File.mkDir(); //建立文件夾
File.isDirectory(); //判斷是文件或文件夾
File[] files = File.listFiles(); //列出文件夾下的所有文件和文件夾名
File.renameTo(dest); //修改文件夾和文件名
File.delete(); //刪除文件夾或文件
六、使用RandomAccessFile進行文件的讀寫:
RandomAccessFile的使用方法比較靈活,功能也比較多,可以使用類似seek的方式可以跳轉到文件的任意位置,從文件指示器當前位置開始讀寫。
它有兩種構造方法
new RandomAccessFile(f,"rw");//讀寫方式
new RandomAccessFile(f,"r");//只讀方式
使用事例:
/*
* 程序功能:演示了RandomAccessFile類的操作,同時實現了一個文件復制操作。
*/
import java.io.*;
public class RandomAccessFileDemo {
public static void main(String[] args) throws Exception {
RandomAccessFile file = new RandomAccessFile("file", "rw");
// 以下向file文件中寫數據
file.writeInt(20);// 佔4個位元組
file.writeDouble(8.236598);// 佔8個位元組
file.writeUTF("這是一個UTF字元串");// 這個長度寫在當前文件指針的前兩個位元組處,可用readShort()讀取
file.writeBoolean(true);// 佔1個位元組
file.writeShort(395);// 佔2個位元組
file.writeLong(2325451l);// 佔8個位元組
file.writeUTF("又是一個UTF字元串");
file.writeFloat(35.5f);// 佔4個位元組
file.writeChar('a');// 佔2個位元組
file.seek(0);// 把文件指針位置設置到文件起始處
// 以下從file文件中讀數據,要注意文件指針的位置
System.out.println("——————從file文件指定位置讀數據——————");
System.out.println(file.readInt());
System.out.println(file.readDouble());
System.out.println(file.readUTF());
file.skipBytes(3);// 將文件指針跳過3個位元組,本例中即跳過了一個boolean值和short值。
System.out.println(file.readLong());
file.skipBytes(file.readShort()); // 跳過文件中「又是一個UTF字元串」所佔位元組,注意readShort()方法會移動文件指針,所以不用加2。
System.out.println(file.readFloat());
//以下演示文件復制操作
System.out.println("——————文件復制(從file到fileCopy)——————");
file.seek(0);
RandomAccessFile fileCopy=new RandomAccessFile("fileCopy","rw");
int len=(int)file.length();//取得文件長度(位元組數)
byte[] b=new byte[len];
file.readFully(b);
fileCopy.write(b);
System.out.println("復制完成!");
}
}
七、讀取資源文件時能否實現類似於seek的方式可以跳轉到文件的任意位置,從指定的位置開始讀取指定的位元組數呢?
答案是可以的。
在FileInputStream和InputStream中都有下面的函數:
public long skip (long byteCount); //從數據流中跳過n個位元組
public int read (byte[] buffer, int offset, int length); //從數據流中讀取length數據存在buffer的offset開始的位置。offset是相對於buffer的開始位置的,不是數據流。
可以使用這兩個函數來實現類似於seek的操作,請看下面的測試代碼:
//其中read_raw是一個txt文件,存放在raw目錄下。
//read_raw.txt文件的內容是:"ABCDEFGHIJKLMNOPQRST"
public String getRawString() throws IOException {
String str = null;
InputStream in = getResources().openRawResource(R.raw.read_raw);
int length = in.available();
byte[] buffer = new byte[length];
in.skip(2); //跳過兩個位元組
in.read(buffer,0,3); //讀三個位元組
in.skip(3); //跳過三個位元組
in.read(buffer,0,3); //讀三個位元組
//最後str="IJK"
str = EncodingUtils.getString(buffer, "BIG5");
in.close();
return str;
}
可以使用這兩個函數來實現類似於seek的操作,請看下面的測試代碼:
從上面的實例可以看出skip函數有點類似於C語言中的seek操作,但它們之間有些不同。
需要注意的是:
1、skip函數始終是從當前位置開始跳的。在實際應用當中還要再判斷一下該函數的返回值。
2、read函數也始終是當前位置開始讀的。
3、另外,還可以使用reset函數將文件的當前位置重置為0,也就是文件的開始位置。
如何得到文件的當前位置?
我沒有找到相關的函數和方法,不知道怎麼樣才能得到文件的當前位置,貌似它也並不是太重要。
八、APK資源文件的大小不能超過1M,如果超過了怎麼辦?我們可以將這個數據再復制到data目錄下,然後再使用。復制數據的代碼如下:
public boolean assetsCopyData(String strAssetsFilePath, String strDesFilePath){
boolean bIsSuc = true;
InputStream inputStream = null;
OutputStream outputStream = null;
File file = new File(strDesFilePath);
if (!file.exists()){
try {
file.createNewFile();
Runtime.getRuntime().exec("chmod 766 " + file);
} catch (IOException e) {
bIsSuc = false;
}
}else{//存在
return true;
}
try {
inputStream = getAssets().open(strAssetsFilePath);
outputStream = new FileOutputStream(file);
int nLen = 0 ;
byte[] buff = new byte[1024*1];
while((nLen = inputStream.read(buff)) > 0){
outputStream.write(buff, 0, nLen);
}
//完成
} catch (IOException e) {
bIsSuc = false;
}finally{
try {
if (outputStream != null){
outputStream.close();
}
if (inputStream != null){
inputStream.close();
}
} catch (IOException e) {
bIsSuc = false;
}
}
return bIsSuc;
}
總結:
1、apk中有兩種資源文件,使用兩種不同的方式進行打開使用。
raw使用InputStream in = getResources().openRawResource(R.raw.test);
asset使用InputStream in = getResources().getAssets().open(fileName);
這些數據只能讀取,不能寫入。更重要的是該目錄下的文件大小不能超過1M。
同時,需要注意的是,在使用InputStream的時候需要在函數名稱後加上throws IOException。
2、SD卡中的文件使用FileInputStream和FileOutputStream進行文件的操作。
3、存放在數據區(/data/data/..)的文件只能使用openFileOutput和openFileInput進行操作。
或者使用BufferedReader,BufferedWriter 進行讀寫,方便按行操作。
4、RandomAccessFile類僅限於文件的操作,不能訪問其他IO設備。它可以跳轉到文件的任意位置,從當前位置開始讀寫。
5、InputStream和FileInputStream都可以使用skip和read(buffre,offset,length)函數來實現文件的隨機讀取。
G. android閱讀app有關的數據流圖怎麼畫
試試添加監聽addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
H. 請問安卓手機怎麼么開啟數據流量
以三星 s6手機為例
操作步驟
1、開始,先在手機的桌面上找到應用程序「設置」圖標,點擊進入新的操作界面。
I. Android開發中數據的完整傳輸流程
簡單流程:打開一個展示伺服器數據的界面後,因為連接伺服器是耗時操作,所以不能在主線程中進行,所以建立一個多線程,在新啟動的線程中根據伺服器定義的通訊協議,連接伺服器,對指定伺服器介面發出Get/Post請求,伺服器根據請求的內容查詢資料庫並封裝數據(通常為xml或json),回傳給客戶端,客戶端解析數據並展示到界面。 這是通常情況下的簡單流程。
在這個過程中,客戶端方面,數據是一直在內存里處理的,如果想長久保留數據需要對數據做持久化(也可以說緩存),也就是建立本地資料庫或者本地文件,如果開發人員不做處理的話數據是不會自動緩存。
緩存到本地是數據的持久化,跟Service沒必然聯系。。。Service是做後台持續性工作的。
純手打~
J. Android 感測器的 數據流和框架 是怎麼樣讓 屏幕旋轉
這篇文章寫的感測器數據從驅動傳遞到應用程序的整個流程,還有數據校正的問題。
應用程序怎麼樣設置可以讓自己隨著設備的傾斜度變化而旋轉方向呢?在AndroidManifest.xml文件中的android:screenOrientation就可以了。這里追蹤一下它的內部機制。
先看一個最關鍵的部件:/frameworks/base/core/java/android/view/WindowOrientationListener.java
這個介面注冊一個accelerator,並負責把accelerator的數據轉化為orientation。這個API對應用程序不公開,我看Android2.3的源碼時發現只有PhoneWindowManager使用到它了。
/frameworks/base/policy/../PhoneWindowManager.java
PhonwWindowManager注冊了一個WindowOrientationListener,就可以非同步獲取當前設備的orientation了。再結合應用程序在AndroidManifest.xml中設置的值來管理著應用程序界面的旋轉方向。以下是PhoneWindowManager.java中相關的兩個代碼片段。
[java] view plain
public void onOrientationChanged(int rotation) {
// Send updates based on orientation value
if (localLOGV) Log.v(TAG, "onOrientationChanged, rotation changed to " +rotation);
try {
mWindowManager.setRotation(rotation, false,
mFancyRotationAnimation);
} catch (RemoteException e) {
// Ignore
}
}
... ...
switch (orientation) {//這個值就是當前設備屏幕的旋轉方向,再結合應用程序設置的android:configChanges屬性值就可以確定應用程序界面的旋轉方向了。應用程序設置值的優先順序大於感測器確定的優先順序。
case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT:
//always return portrait if orientation set to portrait
return mPortraitRotation;
case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE:
//always return landscape if orientation set to landscape
return mLandscapeRotation;
case ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT:
//always return portrait if orientation set to portrait
return mUpsideDownRotation;
case ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE:
//always return seascape if orientation set to reverse landscape
return mSeascapeRotation;
case ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE:
//return either landscape rotation based on the sensor
mOrientationListener.setAllow180Rotation(
isLandscapeOrSeascape(Surface.ROTATION_180));
return getCurrentLandscapeRotation(lastRotation);
case ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT:
mOrientationListener.setAllow180Rotation(
!isLandscapeOrSeascape(Surface.ROTATION_180));
return getCurrentPortraitRotation(lastRotation);
}
讓應用程序隨屏幕方向自動旋轉的實現原理就這么交待完了。我解決這一步時也沒有費多少力氣,在板子上打開SensorTest,對比一下XYZ三個軸和MileStone上面的數據,修改一下正負值就可以了。但要解決Teeter運行時Z軸反轉的問題,還得深層次挖一挖。
PhoneWindowManager.java中有這么一句:
mWindowManager.setRotation(rotation, false, mFancyRotationAnimation);
當PhonewindowManager通過WindowOrientationListener這個監聽器得知屏幕方向改變時,會通知給WindowManagerService(/frameworks/base/service/../WindowManagerService.java)
WindowManagerService中有這么一個監聽器集合:mRotationWatchers,誰想監聽屏幕方向改變,就會在這里注冊一個監聽器。SensorManager就這么幹了。然後,通過下面這個非同步方法獲知當前的屏幕方向