gpt4 book ai didi

android - 使用我的联系人列表中的服务向所有使用 whatsapp 的联系人发送消息

转载 作者:行者123 更新时间:2023-11-29 17:27:09 25 4
gpt4 key购买 nike

我正在尝试做的事情:

  • 我正在尝试向所有正在使用的联系人发送消息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;
}
}

list .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 Activity 方法中,我将启动服务,

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);

最佳答案

在 list 中具有 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);
}

关于android - 使用我的联系人列表中的服务向所有使用 whatsapp 的联系人发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34260460/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com