Last active
May 11, 2019 10:59
-
-
Save pkavoo/ce448c0d107693d60e08a895b4806eba to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.Manifest; | |
import android.content.Intent; | |
import android.content.pm.PackageManager; | |
import android.net.Uri; | |
import android.os.Bundle; | |
import android.provider.ContactsContract; | |
import android.provider.MediaStore; | |
import android.provider.Telephony; | |
import android.support.annotation.NonNull; | |
import android.support.annotation.RequiresApi; | |
import android.support.v7.app.AppCompatActivity; | |
import android.view.View; | |
import android.widget.Toast; | |
import static android.os.Build.VERSION; | |
import static android.os.Build.VERSION_CODES; | |
public class MainActivity extends AppCompatActivity { | |
private static final int CAMERA_REQUEST_CODE = 100; | |
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE=1; | |
String smsText = "This is a test message"; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
} | |
@RequiresApi(api = VERSION_CODES.M) | |
public void openCamera(View view) { | |
if (checkSelfPermission(Manifest.permission.CAMERA) | |
!= PackageManager.PERMISSION_GRANTED) { | |
requestPermissions(new String[]{Manifest.permission.CAMERA}, | |
CAMERA_REQUEST_CODE); | |
}else { // Do stuff here for Action Image Capture. | |
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); | |
// start the image capture Intent | |
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);} | |
} | |
public void openMessage(View view) { | |
if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) //At least KitKat | |
{ | |
String defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(getApplicationContext()); //Need to change the build to API 19 | |
Intent sendIntent = new Intent(Intent.ACTION_SEND); | |
sendIntent.setType("text/plain"); | |
sendIntent.putExtra(Intent.EXTRA_TEXT, smsText); | |
if (defaultSmsPackageName != null)//Can be null in case that there is no default, then the user would be able to choose any app that support this intent. | |
{ | |
sendIntent.setPackage(defaultSmsPackageName); | |
} | |
getApplicationContext().startActivity(sendIntent); | |
} else //For early versions, do what worked for you before. | |
{ | |
Intent sendIntent = new Intent(Intent.ACTION_VIEW); | |
sendIntent.setData(Uri.parse("sms:")); | |
sendIntent.putExtra("sms_body", smsText); | |
getApplicationContext().startActivity(sendIntent); | |
} | |
} | |
public void openContacts(View view) { | |
Intent intent = new Intent(Intent.ACTION_PICK); | |
intent.setType(ContactsContract.Contacts.CONTENT_TYPE); | |
startActivityForResult(intent, 10011); | |
} | |
public void openCalendar(View view) { | |
Intent calendarIntent = new Intent(Intent.ACTION_EDIT); | |
calendarIntent.setType("vnd.android.cursor.item/event"); | |
calendarIntent.putExtra("title", "Title"); | |
calendarIntent.putExtra("beginTime", 2000); | |
calendarIntent.putExtra("endTime", 5000); | |
calendarIntent.putExtra("description", "Description"); | |
startActivity(calendarIntent); | |
} | |
public void openBrowser(View view) { | |
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")); | |
startActivity(browserIntent); | |
} | |
@Override | |
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { | |
super.onRequestPermissionsResult(requestCode, permissions, grantResults); | |
if (requestCode == CAMERA_REQUEST_CODE) { | |
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { | |
Toast.makeText(this, "Camera permission granted.", Toast.LENGTH_LONG).show(); | |
// Do stuff here for Action Image Capture. | |
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); | |
// start the image capture Intent | |
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE); | |
} else { | |
Toast.makeText(this, "Camera permission denied.", Toast.LENGTH_LONG).show(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment