Skip to content

Commit

Permalink
IntentTest
Browse files Browse the repository at this point in the history
  • Loading branch information
yangzhiying committed Dec 26, 2015
1 parent 469a228 commit 016138a
Show file tree
Hide file tree
Showing 27 changed files with 706 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
27 changes: 27 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.2"

defaultConfig {
applicationId "com.glimmer.intenttest"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
useLibrary 'org.apache.http.legacy'
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
}
17 changes: 17 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /Users/glimmer/devTools/android-sdk-macosx/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.glimmer.intenttest;

import android.app.Application;
import android.test.ApplicationTestCase;

/**
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
*/
public class ApplicationTest extends ApplicationTestCase<Application> {
public ApplicationTest() {
super(Application.class);
}
}
38 changes: 38 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.glimmer.intenttest">

<uses-permission
android:name="android.permission.INTERNET"/>
<uses-permission
android:name="android.permission.SEND_RESPOND_VIA_MESSAGE"/>
<uses-permission
android:name="android.permission.SEND_SMS"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"/>
<data android:mimeType="image/*"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SENDTO"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:scheme="sms"/>
<data android:scheme="smsto"/>
</intent-filter>

</activity>
<activity android:name=".SecondActivity"/>
</application>

</manifest>
182 changes: 182 additions & 0 deletions app/src/main/java/com/glimmer/intenttest/MainActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
package com.glimmer.intenttest;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.CalendarContract;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;
import java.util.Calendar;
import java.util.List;
import org.apache.http.protocol.HTTP;

public class MainActivity extends Activity implements View.OnClickListener {
private static final int PICK_CONTACT_REQUEST = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button btn_call = (Button) findViewById(R.id.btn_call);
Button btn_map = (Button) findViewById(R.id.btn_map);
Button btn_web = (Button) findViewById(R.id.btn_web);
Button btn_send_email = (Button) findViewById(R.id.btn_send_email);
Button btn_create_calendar = (Button) findViewById(R.id.btn_create_calendar);
Button btn_pick_contact = (Button) findViewById(R.id.btn_pick_contact);
ImageButton ibtn_picture = (ImageButton) findViewById(R.id.ibtn_picture);

btn_call.setOnClickListener(this);
btn_map.setOnClickListener(this);
btn_web.setOnClickListener(this);
btn_send_email.setOnClickListener(this);
/**
* 添加日期备注
*/
btn_create_calendar.setOnClickListener(this);
btn_pick_contact.setOnClickListener(this);

Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
if (action.equals(Intent.ACTION_SEND)) {
if (type.startsWith("image/")) {
//handle sent text
Uri data = intent.getParcelableExtra(Intent.EXTRA_STREAM);
if (data != null) {
if (intent.getType().contains("image/")) {
Toast.makeText(MainActivity.this, "ok", Toast.LENGTH_SHORT).show();
ibtn_picture.setImageURI(data);
}
}
} else if (type.startsWith("text/")) {
//handle sent image
String text = intent.getParcelableExtra(Intent.EXTRA_TEXT);
if (text != null) {
Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();

}
}
} else if (action.equals(Intent.ACTION_MAIN)) {
Toast.makeText(MainActivity.this, "error", Toast.LENGTH_SHORT).show();
}

}


@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_call:
call();
break;
case R.id.btn_map:
map("geo:0,0?q=1600+Amphitheatre+Parkway,+Mountain+View,+California");
break;
case R.id.btn_web:
web("http://www.android.com");
break;
case R.id.btn_create_calendar:
calendar();
break;
case R.id.btn_send_email:
sendEmail();
break;
case R.id.btn_pick_contact:
pickContact();
break;
default:
break;

}
}

/**
* 从content provider里获取数据 如何从被选的联系人中读出电话号码
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == PICK_CONTACT_REQUEST) {
if (resultCode == RESULT_OK) {

Uri contactUri = data.getData();
String[] projection = {ContactsContract.CommonDataKinds.Phone.NUMBER};
Cursor cursor = getContentResolver().query(contactUri, projection, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
int column = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String number = cursor.getString(column);
Toast.makeText(MainActivity.this, number, Toast.LENGTH_SHORT).show();
cursor.close();
}
}
}
}

private void calendar() {
Intent calendarIntent = new Intent(Intent.ACTION_INSERT, CalendarContract.Events.CONTENT_URI);
Calendar beginTime = Calendar.getInstance();
beginTime.set(2012, 0, 19, 7, 30); //2012.1.19 7:30-10:30
Calendar endTime = Calendar.getInstance();
endTime.set(2012, 0, 19, 10, 30);
calendarIntent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, beginTime.getTimeInMillis());
calendarIntent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endTime.getTimeInMillis());
calendarIntent.putExtra(CalendarContract.Events.TITLE, "Glimmer class");
calendarIntent.putExtra(CalendarContract.Events.EVENT_LOCATION, "Secret dojo");
startActivity(calendarIntent);
}

private void sendEmail() {
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType(HTTP.PLAIN_TEXT_TYPE);
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Email subject");
//发送的消息
emailIntent.putExtra(Intent.EXTRA_TEXT, "Email message text");
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("content://path/to/email/attachment"));
String title = (String) getResources().getText(R.string.app_name);
//指定显示的标题
Intent chooser = Intent.createChooser(emailIntent, title);
startActivity(chooser);
}

private void web(String uriString) {
Uri webpage = Uri.parse(uriString);
Intent webIntent = new Intent(Intent.ACTION_VIEW, webpage);
startActivity(webIntent);
}

private void map(String uriString) {
Uri location = Uri.parse(uriString);
Intent locationIntent = new Intent(Intent.ACTION_VIEW, location);
startActivity(locationIntent);
}

private void call() {
Uri number = Uri.parse("tel:15019494700");
Intent intent = new Intent(Intent.ACTION_DIAL, number);
//判断是否有app去接受这intent防止app crash
PackageManager packageManager = getPackageManager();
List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0);
boolean isIntentSafe = activities.size() > 0;
if (isIntentSafe) {
startActivity(intent);
}
}

private void pickContact() {
Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts"));
pickContactIntent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
}

}
15 changes: 15 additions & 0 deletions app/src/main/java/com/glimmer/intenttest/SecondActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.glimmer.intenttest;

import android.app.Activity;
import android.os.Bundle;

/**
* Created by glimmer on 15/12/22.
*/
public class SecondActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
}
61 changes: 61 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btn_call"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打电话"
android:textColor="#FF83FA"
android:textSize="16sp"
android:layout_margin="10dp"/>
<Button
android:id="@+id/btn_map"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查看地图"
android:textColor="#FF83FA"
android:textSize="16sp"
android:layout_margin="10dp"/>
<Button
android:id="@+id/btn_web"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查看网页"
android:textColor="#FF83FA"
android:textSize="16sp"
android:layout_margin="10dp"/>
<Button
android:id="@+id/btn_send_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送一个带附件的email"
android:textColor="#FF83FA"
android:textSize="16sp"
android:layout_margin="10dp"/>
<Button
android:id="@+id/btn_create_calendar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="创建一个日历事件"
android:textColor="#FF83FA"
android:textSize="16sp"
android:layout_margin="10dp"/>
<Button
android:id="@+id/btn_pick_contact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打开联系人"
android:textColor="#FF83FA"
android:textSize="16sp"
android:layout_margin="10dp"/>
<ImageButton
android:id="@+id/ibtn_picture"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#FF83FA"
android:layout_margin="10dp"/>
</LinearLayout>
8 changes: 8 additions & 0 deletions app/src/main/res/layout/activity_second.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

</LinearLayout>
Binary file added app/src/main/res/mipmap-hdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/mipmap-mdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/mipmap-xhdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/mipmap-xxhdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions app/src/main/res/values-w820dp/dimens.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<resources>
<!-- Example customization of dimensions originally defined in res/values/dimens.xml
(such as screen margins) for screens with more than 820dp of available width. This
would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively). -->
<dimen name="activity_horizontal_margin">64dp</dimen>
</resources>
6 changes: 6 additions & 0 deletions app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
</resources>
5 changes: 5 additions & 0 deletions app/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
</resources>
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<resources>
<string name="app_name">IntentTest</string>
</resources>
Loading

0 comments on commit 016138a

Please sign in to comment.