Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
362 views
in Technique[技术] by (71.8m points)

java - Android - Send Telegram message to a specific number

I'm trying to send a Telegram message to a specific number from within my Android app. Right now my code launches Telegram app, and then the user has to select the destinatary. What I want to do is to send the message to the specified number, without having the user select the contact. My code is as follows:

/**
 * Intent to send a telegram message
 * @param msg
 */
void intentMessageTelegram(String msg)
{
    final String appName = "org.telegram.messenger";
    final boolean isAppInstalled = isAppAvailable(mUIActivity.getApplicationContext(), appName);
    if (isAppInstalled) 
    {
        Intent myIntent = new Intent(Intent.ACTION_SEND);
        myIntent.setType("text/plain");
        myIntent.setPackage(appName);
        myIntent.putExtra(Intent.EXTRA_TEXT, msg);//
        mUIActivity.startActivity(Intent.createChooser(myIntent, "Share with"));
    } 
    else 
    {
        Toast.makeText(mUIActivity, "Telegram not Installed", Toast.LENGTH_SHORT).show();
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can't send to special number, But You can do this by USERID

try {

    Intent telegramIntent = new Intent(Intent.ACTION_VIEW);
    telegramIntent.setData(Uri.parse("http://telegram.me/USERID"));
    startActivity(telegramIntent);

} catch (Exception e) {
        // show error message
}

This code will show user an alert for choosing applications that support telegram uri's like Telegram itself and Mobogram!

Tip: don't set package name. some people install telegram alternatives like mobogram.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...