Monday, January 19, 2015

Android sending text message with attachment

package com.example.peble;

import java.io.FileOutputStream;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.location.Location;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.NavUtils;
import android.telephony.SmsManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.maps.MapActivity;

public class DisplayMessageActivity extends MapActivity {
 String message="";
 LocationManager locationManager; //<2>  
 Uri currImageURI;
 String smsText="";
 @Override
 protected boolean isRouteDisplayed() {
  return false;
 }
 @Override
 protected void onStart() {
  super.onStart();
  message="";
 }
 @Override
 protected void onRestart() {
  super.onRestart();
  message="";
 }
 public void onRestoreInstanceState(Bundle savedInstanceState) {    
  // Always call the superclass so it can restore the view hierarchy    
  super.onRestoreInstanceState(savedInstanceState);       // Restore state members from saved instance   
 }
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  if (savedInstanceState != null) {
   message = savedInstanceState.getString("message");
  }
    // Get the message from the intent
     Intent intent = getIntent();
     if (message.trim().equals(""))
     message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
     Toast.makeText(getApplicationContext(),
    "message="+message ,
    Toast.LENGTH_LONG).show();
     String filename = "peble_contacts.txt";
     FileOutputStream outputStream;
     try {
      outputStream = openFileOutput(filename, Context.CONTEXT_IGNORE_SECURITY);
      outputStream.write(message.getBytes());
      outputStream.close();
     } catch (Exception e) {
      System.out.println("cannot read file");
     }
     smsText="Please help!";
     try {
      locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
   Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
   
         if (location != null) {
             smsText += String.format(
                     "Current Location \n Longitude: %1$s \n Latitude: %2$s",
                     location.getLongitude(), location.getLatitude()
             );
         }
     } catch (Exception e) {
      Toast.makeText(getApplicationContext(),
     "Location faild, please try again later!",
     Toast.LENGTH_LONG).show();
     }
     try {
//   SmsManager smsManager = SmsManager.getDefault();
//   smsManager.sendTextMessage(message, null, smsText, null, null);
//   Intent sendIntent = new Intent(Intent.ACTION_SEND); 
//      Intent sendIntent = new Intent(Intent.ACTION_VIEW);
      
   //sendIntent.setClassName("com.android.mms", "com.android.mms.ui.ComposeMessageActivity");
//      sendIntent.setType("vnd.android-dir/mms-sms");
//   sendIntent.putExtra("address", message);
//   sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///users/public/pictures/sample pictures/chrysanthemum.jpg"));
//      Runnable myRunnable = new Thread(new Runnable() { public void run() {
//       Intent picIntent = new Intent();
//    picIntent.setType("image/*");
//    picIntent.setAction(Intent.ACTION_GET_CONTENT); 
//    startActivityForResult(Intent.createChooser(picIntent, "Select Picture"),1); 
//     this.notify();
//    }});
//      synchronized(myRunnable) {
//       runOnUiThread(myRunnable);
//       myRunnable.wait();
//      }
////   new Thread(new Runnable() { public void run() {
//    Uri uri = Uri.parse("smsto:" + message);
//       Intent sendIntent = new Intent(Intent.ACTION_SENDTO, uri);
//    sendIntent.putExtra(Intent.EXTRA_STREAM, currImageURI);
//    sendIntent.putExtra("sms_body", smsText); 
//    startActivity(sendIntent);
////   }}).start();
//   Toast.makeText(getApplicationContext(), smsText + " Sent!",
//      Toast.LENGTH_LONG).show();
   //sending audio or video
   /*
    * Intent sendIntent = new Intent(Intent.ACTION_SEND); 
sendIntent.setClassName("com.android.mms", "com.android.mms.ui.ComposeMessageActivity");
sendIntent.putExtra("address", "1213123123");
sendIntent.putExtra("sms_body", "if you are sending text");   
final File file1 = new File(mFileName);
if(file1.exists()){
  System.out.println("file is exist");
}
Uri uri = Uri.fromFile(file1);
sendIntent.putExtra(Intent.EXTRA_STREAM, uri);
sendIntent.setType("video/*");
startActivity(sendIntent);
    */
    } catch (Exception e) {
   Toast.makeText(getApplicationContext(),
    "SMS faild, please try again later!" + e.getMessage(),
    Toast.LENGTH_LONG).show();
   e.printStackTrace();
    }
     // Create the text view
     TextView textView = new TextView(this);
     textView.setTextSize(40);
     textView.setText(message + " will be sent your help Message: " + smsText);
     // Set the text view as the activity layout
//     setContentView(textView);
     setContentView(R.layout.activity_display_message);
 }

 /**
  * Set up the {@link android.app.ActionBar}, if the API is available.
  */
 @TargetApi(Build.VERSION_CODES.HONEYCOMB)
 private void setupActionBar() {
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
   getActionBar().setDisplayHomeAsUpEnabled(true);
  }
 }

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

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
  switch (item.getItemId()) {
  case android.R.id.home:
   // This ID represents the Home or Up button. In the case of this
   // activity, the Up button is shown. Use NavUtils to allow users
   // to navigate up one level in the application structure. For
   // more details, see the Navigation pattern on Android Design:
   //
   // http://developer.android.com/design/patterns/navigation.html#up-vs-back
   //
   NavUtils.navigateUpFromSameTask(this);
   return true;
  }
  return super.onOptionsItemSelected(item);
 }
 // To handle when an image is selected from the browser, add the following to your Activity 
 @Override 
 public void onActivityResult(int requestCode, int resultCode, Intent data) { 
  
  if (resultCode == RESULT_OK) { 
  
  if (requestCode == 1) { 
  
  // currImageURI is the global variable I’m using to hold the content:// URI of the image 
  currImageURI = data.getData(); 
  } 
  } 
 } 
  
 // And to convert the image URI to the direct file system path of the image file 
 public String getRealPathFromURI(Uri contentUri) { 
  
  // can post image 
  String [] proj={MediaStore.Images.Media.DATA}; 
  Cursor cursor = managedQuery( contentUri, 
  proj, // Which columns to return 
  null, // WHERE clause; which rows to return (all rows) 
  null, // WHERE clause selection arguments (none) 
  null); // Order-by clause (ascending by name) 
  int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
  cursor.moveToFirst(); 
  
  return cursor.getString(column_index); 
 } 
 /** Called when the user clicks the Send button */
 public void attachImage(View view) {
     // Do something in response to button
  Intent picIntent = new Intent();
  picIntent.setType("image/*");
  picIntent.setAction(Intent.ACTION_GET_CONTENT); 
  startActivityForResult(Intent.createChooser(picIntent, "Select Picture"),1); 
 }
 public void sendText(View view) {
  Intent intent = getIntent();
     message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
  Toast.makeText(getApplicationContext(),
    "phonenm=" + message ,
    Toast.LENGTH_LONG).show();
  Uri uri = Uri.parse("smsto:" + message);
     Intent sendIntent = new Intent(Intent.ACTION_SENDTO, uri);
//     sendIntent.setType("vnd.android-dir/mms-sms");
  sendIntent.putExtra("sms_body", smsText);
  sendIntent.putExtra("address", message);
  sendIntent.setData(Uri.parse("smsto:" + message)); 
  if (currImageURI != null)
  sendIntent.putExtra(Intent.EXTRA_STREAM, currImageURI);
  startActivity(sendIntent);
  Toast.makeText(getApplicationContext(),
    "SMS success" ,
    Toast.LENGTH_LONG).show();
 }
}


1 comment:

  1. Yeah!! Send SMS Attachment has enabled business organizations to share files, brochures, and menus through SMS just the way they did it with an email. Users get the flexibility of attaching files to their bulk messages. Contact us: +91-8349217770 or Sales@Msgclub.Net / Sales@smsprovider.com

    ReplyDelete