J'ai un widget et je veux qu'il fasse un appel téléphonique à un numéro particulier lorsque l'utilisateur clique sur le widget. Comment puis-je faire cela? S'il vous plaît aider.Android: Faire un appel à partir du widget écran d'accueil
Répondre
j'ai pu le faire fonctionner avec ce code:
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
Log.d(LOG_TAG, "onUpdate(): ");
for (int appWidgetId : appWidgetIds) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:"+number));
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, callIntent, 0);
// Get the layout for the App Widget and attach an on-click listener to the button
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.caller);
views.setOnClickPendingIntent(R.id.callButton, pendingIntent);
// Tell the AppWidgetManager to perform an update on the current App Widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
public static Intent newPhoneCallIntent(String phoneNumber){
Intent callintent = new Intent(Intent.ACTION_DIAL);
callintent.setData(Uri.parse("tel:"+phoneNumber));
return callintent;
}
startActivity(newPhoneCallIntent("5555555555"));
où dois-je ajouter cela? Dans la fonction 'onUpdate()'? –
'startActivity()' renvoie l'erreur - 'La méthode startActivity (Intent) est indéfinie pour le type Widget' –
@rohith vous devez le faire depuis un contexte. Si vous le faites depuis un bouton quelconque, essayez getContext(). StartActivity() – schwiz
cela fonctionne-t-il la première fois que vous ajoutez le widget? ou devez-vous lancer l'application en premier pour déclencher une mise à jour? – Joset
cela fonctionnera la première fois. 'onUpdate()' est appelé lorsque vous ajoutez le widget. –