How to schedule a message in our application to WhatsApp?

本小妞迷上赌 提交于 2019-12-08 14:00:12

问题


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

Currently, I can open WhatsApp using this code

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

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


回答1:


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

    }
}

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

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

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

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



回答2:


But i would like to know how to schedule message from our application to what'sapp

No, there is no such API till now



来源:https://stackoverflow.com/questions/43061919/how-to-schedule-a-message-in-our-application-to-whatsapp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!