如何在我们的应用程序中将消息安排到 WhatsApp? [英] How to schedule a message in our application to WhatsApp?

查看:32
本文介绍了如何在我们的应用程序中将消息安排到 WhatsApp?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过我的应用程序在 WhatsApp 中发送和安排短信.可以这样做吗?

I would like to send, schedule text messages in WhatsApp from my application. Is it possible to do that?

目前,我可以使用此代码打开 WhatsApp

Currently, I can open WhatsApp using this code

Intent i=getpackageManager().getLaunchIntentForPackage("com.whatsapp");
startActivity(i);

但是,是否可以安排从我们的应用程序到 WhatsApp 的消息?

However, is it possible to schedule a message from our application to WhatsApp?

推荐答案

您可以使用 AlarmManager 来安排未来的任何任务..在您的 Activity/Fragment 中,使用这行代码来安排任何任务:-

You can use the AlarmManager for schedule any task for the future.. In your Activity/Fragment use this lines of code for schedule any task:-

 Intent myIntent = new Intent(AlaramClass.this, AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(AlaramClass.this, 0, myIntent, 0);
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, "SPECIFY_YOUR_TIME_HERE_TO_SCHEDULE_TASK", pendingIntent);

然后创建接收器来接收未来的任务

And than create the receiver to receive future task

public class AlarmReceiver extends WakefulBroadcastReceiver {
    @Override
    public void onReceive(final Context context, Intent intent) {

        Intent i=getpackageManager().getLaunchIntentForPackage("com.whatsapp");
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(i);

    }
}

并且不要忘记 Manifest 中的 Receiver 条目(在 <application.....</application>)

And do not forget the entry for Receiver inside the Manifest (inside the <application>.....</application>)

          <receiver
            android:name=".AlarmReceiver"
            android:exported="true" >
        </receiver>

您需要为它添加 WAKE_LOCK 权限,如下所示:-

And u need to add the WAKE_LOCK permission for it like below:-

<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>

这篇关于如何在我们的应用程序中将消息安排到 WhatsApp?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆