2015-12-18 21 views
-3

我是Android的新手。我想發送&通過短信分享a TextView的。如何通過短信發送和共享TextView?

我可以通過將文本放入字符串來發送。但我無法使用TextView發送或分享價值。

我該怎麼做?

這裏是我的代碼我在做什麼..

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

    tv = (TextView)findViewById(R.id.textView); 
    final String tv1 = tv.getText().toString(); 
    btn1 = (Button)findViewById(R.id.button); 
    btn1.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent localIntent = new Intent("android.intent.action.VIEW", Uri.parse("tel:")); 
      localIntent.putExtra("sms_body", MainActivity.this.getText(Integer.parseInt(tv1))); 
      localIntent.setType("vnd.android-dir/mms-sms"); 
      MainActivity.this.startActivity(localIntent); 
     } 
    }); 

    btn2 = (Button)findViewById(R.id.button2); 
    btn2.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent localIntent = new Intent("android.intent.action.SEND"); 
      localIntent.setType("text/plain"); 
      localIntent.putExtra("android.intent.extra.TEXT", R.string.app_name); 
      MainActivity.this.startActivity(Intent.createChooser(localIntent, "Share SMS")); 
     } 
    }); 
} 

我得到這個錯誤...

FATAL EXCEPTION: main 
                   Process: nextappsbd.sendshare, PID: 3521 
                   java.lang.NumberFormatException: Invalid int: "Hello World!" 
                    at java.lang.Integer.invalidInt(Integer.java:137) 
                    at java.lang.Integer.parse(Integer.java:374) 
                    at java.lang.Integer.parseInt(Integer.java:365) 
                    at java.lang.Integer.parseInt(Integer.java:331) 
                    at nextappsbd.sendshare.MainActivity$1.onClick(MainActivity.java:27) 
                    at android.view.View.performClick(View.java:4463) 
                    at android.view.View$PerformClick.run(View.java:18792) 
                    at android.os.Handler.handleCallback(Handler.java:808) 
                    at android.os.Handler.dispatchMessage(Handler.java:103) 
                    at android.os.Looper.loop(Looper.java:193) 
                    at android.app.ActivityThread.main(ActivityThread.java:5344) 
                    at java.lang.reflect.Method.invokeNative(Native Method) 
                    at java.lang.reflect.Method.invoke(Method.java:515) 
                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:676) 
                    at dalvik.system.NativeStart.main(Native Method) 
+2

約得到什麼來自'TextView'的字符串值,然後發送/共享它?否則,您無法發送/共享視圖。 – Rami

+0

我對此沒有興趣 –

+2

您無法通過短信共享視圖(您的案例中的TextView)。 – Rami

回答

-1
Intent message = new Intent(Intent.ACTION_VIEW, Uri.parse("sms:" + "")); 
       message.putExtra("sms_body",Textview.gettext.toString()); 
       startActivity(message); 

試試這個

+0

它不起作用 –

+0

請發佈您的代碼,以便它可以幫助我們獲得解決方案 – nakul

+0

我發佈了它.... –

0
tv = (TextView)findViewById(R.id.textView); 
    final String tv1 = tv.getText().toString(); 
    btn1 = (Button)findViewById(R.id.button); 
    btn1.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent localIntent = new Intent("android.intent.action.VIEW", Uri.parse("tel:")); 
      localIntent.putExtra("sms_body", tv1); 
      localIntent.setType("vnd.android-dir/mms-sms"); 
      MainActivity.this.startActivity(localIntent); 
     } 
    }); 

    btn2 = (Button)findViewById(R.id.button2); 
    btn2.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent localIntent = new Intent("android.intent.action.SEND"); 
      localIntent.setType("text/plain"); 
      localIntent.putExtra("android.intent.extra.TEXT", tv1); 
      MainActivity.this.startActivity(Intent.createChooser(localIntent, "Share SMS")); 
     } 
    }); 
}