【问题标题】:Message to all the contacts that are using whatsapp using a service in my contact list使用我的联系人列表中的服务向所有使用 whatsapp 的联系人发送消息
【发布时间】:2015-12-14 15:53:40
【问题描述】:

我想做的事:

  • 我正在尝试向所有正在使用的联系人发送消息 whatsapp 使用我的联系人列表中的服务
  • 有可能吗?
  • 如果可能怎么做?

我能做什么:我可以启动whats应用程序,我可以向它传递一条短信,然后通过我创建的单个联系人或组发送消息


下载服务.java

public class DownloadService extends Service {

    public static boolean serviceState = false;

    @Override
    public void onCreate() {
        serviceState = true;
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d("SERVICE-ONCOMMAND", "onStartCommand");

        return START_STICKY;
    }
    @Override
    public void onDestroy() {

        Log.d("SERVICE-DESTROY", "DESTORY");
        serviceState = false;
        //Toast.makeText(this, "service done", Toast.LENGTH_SHORT).show();
    }
    @Override
    public IBinder onBind(Intent intent) {
        // We don't provide binding, so return null
        return null;
    }
}

Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.samples.customprogressloader" >

    <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"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service android:name=".DownloadService"
            android:enabled="true" />
    </application>

</manifest>

OnCreate 活动方法中,我正在启动服务,

startService(new Intent(this, DownloadService.class));

I am using this method to send message through whats app from here docs

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(sendIntent);

【问题讨论】:

    标签: android


    【解决方案1】:

    在清单中有READ_CONTACTS 权限。你可以得到使用com.whatsapp的联系方式。

    Cursor c = getContentResolver().query(
            RawContacts.CONTENT_URI,
            new String[] { RawContacts.CONTACT_ID, RawContacts.DISPLAY_NAME_PRIMARY },
            RawContacts.ACCOUNT_TYPE + "= ?",
            new String[] { "com.whatsapp" },
            null);
    
    int contactIDColumn = c.getColumnIndex(RawContacts.CONTACT_ID );
    SmsManager smsManager = SmsManager.getDefault(); // **SEND_SMS** permission needed
    while (c.moveToNext())
    {
      String whatsAppContact =   c.getString(contactIDColumn);
      smsManager.sendTextMessage(whatsAppContact, null, message, null, null);
    }
    

    【讨论】:

    • 关于 SMS 逻辑,如果您有 10 个条目作为答案...我必须启动什么应用程序 10 次 .... 有没有办法可以向所有 10 个发送消息- 通过whatsapp进入?
    • 您要发送 SMS 或 Whatsapp 文本?
    • 您不能在 Whatsapp 中以编程方式发送批量消息。
    • 谢谢 [+1] .... 我很清楚我们不能通过 whats 应用发送批量消息 .... 但它是如何通过短信完成的,任何现有的 Stackoverflow 答案
    • 如果需要,您可以发送短信我可以编辑我的答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-24
    • 2015-12-21
    • 2017-04-29
    • 2023-03-16
    • 2022-11-15
    • 1970-01-01
    • 2020-06-14
    相关资源
    最近更新 更多