Skip to content

Commit

Permalink
Revert "Revert "hzuapps#93 #71监听短信内容成功,获取Json数据""
Browse files Browse the repository at this point in the history
This reverts commit 86d5cb5.
  • Loading branch information
zengyt committed Jun 15, 2016
1 parent 86d5cb5 commit d7618ed
Show file tree
Hide file tree
Showing 3 changed files with 196 additions and 187 deletions.
175 changes: 13 additions & 162 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,178 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="edu.hzuapps.androidworks">
package="com.example.contentprovider"
android:versionCode="1"
android:versionName="1.0" >

<!-- 使用外部存储的权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<!-- 访问网络的权限 -->
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>


<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name">
android:name="com.example.contentprovider.Com1314080901201Activity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".homeworks.BackActivity"
android:label="@string/title_activity_back">
</activity>
<activity
android:name=".examples.ViewBasicActivity"
android:label="@string/title_activity_view_basic">
</activity>
<activity
android:name=".examples.ActivityBasicActivity"
android:label="@string/title_activity_activity_basic">
</activity>
<activity
android:name=".examples.ActivityBasic2Activity"
android:label="@string/title_activity_activity_basic2">
</activity>
<activity
android:name=".examples.FragmentDemoActivity"
android:label="@string/title_activity_fragment">
</activity>
<activity
android:name=".examples.FragmentDemoContentActivity"
android:label="@string/title_activity_fragment_content">
</activity>
<activity
android:name=".homeworks.net123456.Net123456Activity"
android:label="@string/title_activity_net123456">
</activity>
<activity
android:name=".homeworks.com123456.Com123456Activity"
android:label="@string/title_activity_com123456">
</activity>
<activity
android:name=".examples.ServiceDemoActivity"
android:label="@string/title_activity_service_demo">
</activity>

<service
android:name=".examples.ServiceDemo"
android:enabled="true"
android:exported="true">
</service>
<service
android:name=".examples.IntentServiceDemo"
android:exported="false">
</service>

<activity
android:name=".examples.BroadcastReceiverActivity"
android:label="@string/title_activity_broadcast_receiver">
</activity>

<receiver
android:name=".examples.BroadcastReceiverDemo"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="edu.androidlabs.DETECTION"/>
</intent-filter>
</receiver>

<activity
android:name=".examples.ContentProviderActivity"
android:label="@string/title_activity_content_provider">
</activity>

<provider
android:name=".examples.ContentProviderOfBooks"
android:authorities="edu.hzuapps.androidworks.Library"
android:enabled="true"
android:exported="true">
</provider>

<activity
android:name=".examples.LinearLayoutActivity"
android:label="@string/title_activity_linear_layout">
</activity>
<activity
android:name=".examples.RelativeLayoutActivity"
android:label="@string/title_activity_relative_layout">
</activity>
<activity
android:name=".examples.FrameLayoutActivity"
android:label="@string/title_activity_frame_layout">
</activity>
<activity
android:name=".examples.TableLayoutActivity"
android:label="@string/title_activity_table_layout">
</activity>
<activity
android:name=".examples.AbsoluteLayoutActivity"
android:label="@string/title_activity_absolute_layout">
</activity>
<activity
android:name=".examples.ListViewActivity"
android:label="@string/title_activity_list_view">
</activity>
<activity
android:name=".examples.ViewDemoActivity"
android:label="@string/title_activity_view_demo">
</activity>
<activity
android:name=".examples.GridViewActivity"
android:label="@string/title_activity_grid_view">
</activity>
<activity
android:name=".examples.GridViewOneActivity"
android:label="@string/title_activity_grid_view_one">
</activity>
<activity
android:name=".examples.SharedPreferencesActivity"
android:label="@string/title_activity_shared_preferences">
</activity>
<activity
android:name=".examples.FileStorageActivity"
android:label="@string/title_activity_file_storage">
</activity>

<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="edu.hzuapps.androidworks.FILE_PROVIDER"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"/>
</provider>

<activity
android:name=".examples.FileSelectActivity"
android:label="@string/title_activity_file_select">
<intent-filter>
<action android:name="android.intent.action.PICK"/>

<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.OPENABLE"/>

<data android:mimeType="text/plain"/>
<data android:mimeType="image/*"/>
</intent-filter>
</activity>
<activity
android:name=".examples.FileShareActivity"
android:label="@string/title_activity_file_share">
</activity>
<activity
android:name=".examples.NetworkActivity"
android:label="@string/title_activity_network">
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -1,22 +1,183 @@
package com.example.personalinformation;
package com.example.contentprovider;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.content.ContentResolver;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;

public class Com1314080901201Activity extends Activity {
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import org.json.JSONObject;


import android.os.Message;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class Com1314080901201Activity extends Activity {

private ContentResolver resolver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_com1314080901201);
tv=(TextView) findViewById(R.id.tv);
resolver=getContentResolver();
//arg0表示监听哪一个应用
resolver.registerContentObserver(Uri.parse("content://sms"), true,new MyObserver(new Handler()));
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.com1314080901201, menu);
return true;
}


class MyObserver extends ContentObserver{

public MyObserver(Handler handler) {
super(handler);
// TODO Auto-generated constructor stub
}

//每当短信应用发生改变就调用该方法
@Override
public void onChange(boolean selfChange) {
// TODO Auto-generated method stub
System.out.println("短信应用发生改变");
//进行获取短信应用中的短信操作
Cursor cursor=resolver.query(Uri.parse("content://sms"), new String[]{"address","date","body","type"}, null, null, null);
while(cursor.moveToNext()){
String address=cursor.getString(0);
String date=cursor.getString(1);
String body=cursor.getString(2);
String type=cursor.getString(3);
System.out.println(address+"--"+date+"--"+body+"--"+type);
}
super.onChange(selfChange);
}
}
private final int SUCCESS = 1;
private final int FAILURE = 0;
private final int ERRORCODE = 2;
private TextView tv;

private Handler handler=new Handler(){
public void handleMessage(Message msg){
switch (msg.what) {
case SUCCESS:
String result=getJson(msg.obj.toString());
//主线程更新UI
tv.setText(result);
Toast.makeText(Com1314080901201Activity.this, "获取数据成功", Toast.LENGTH_SHORT)
.show();
break;
case FAILURE:
Toast.makeText(Com1314080901201Activity.this, "获取数据失败", Toast.LENGTH_SHORT)
.show();
case ERRORCODE:
Toast.makeText(Com1314080901201Activity.this, "获取的CODE码不为200!",
Toast.LENGTH_SHORT).show();
default:
break;
}
}
};


public String getJson(String string){
JSONObject jObject= null;
try {
jObject =new JSONObject(string);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("创建JsonObject失败");
}
//解析JSON数据
String city=jObject.optString("city");
String city_en=jObject.optString("city_en");
String date_y=jObject.optString("date_y");
String week=jObject.optString("week");
int cityid=jObject.optInt("cityid");
String temp1=jObject.optString("temp1");
System.out.println(temp1+""+date_y+""+city_en+""+cityid+""+week+""+city);
String weatherResult="城市:"+city+"\n城市英文名:"+city_en+"\n日期:"+date_y+"\n星期:"+week+"\n城市编号:"+cityid+"\n温度:"+temp1;
return weatherResult;
}

public void click(View v){
//开子线程获取网络请求
new Thread(){
int code=0;
public void run() {
// TODO Auto-generated method stub
try {

String path="https://raw.githubusercontent.com/hzuapps/android-labs/master/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901201/city.json";
URL url=new URL(path);
HttpURLConnection conn=(HttpURLConnection) url.openConnection();
//设置GET请求
conn.setRequestMethod("GET");
//设置请求超时时间
conn.setConnectTimeout(5000);
code=conn.getResponseCode();
System.out.println(code);
//200表示获取请求成功
if(code==200){
System.out.println("获取数据成功");
InputStream is=conn.getInputStream();
String result = readMyInputStream(is);

//handle Message
Message msg=new Message();
msg.obj= result;
msg.what=SUCCESS;
handler.sendMessage(msg);
} else {
Message msg = new Message();
msg.what = ERRORCODE;
handler.sendMessage(msg);
}
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("获取数据失败");
e.printStackTrace();
Message msg = new Message();
msg.what = FAILURE;
handler.sendMessage(msg);
}


super.run();
}
}.start();
}

//以流的形式读取数据
public String readMyInputStream(InputStream is){
byte[] result;
try {
ByteArrayOutputStream baos=new ByteArrayOutputStream();
byte[] buffer=new byte[1024];
int len;
while((len = is.read(buffer))!= -1){
baos.write(buffer,0,len);
}
is.close();
baos.close();
result =baos.toByteArray();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
String errorString="获取数据失败";
return errorString;
}
return new String(result);
}
}
Loading

0 comments on commit d7618ed

Please sign in to comment.