Reputation: 31
Complete android development newbie here, be warned.
I'm trying to develop a homescreen widget that allows you to tap the widget to call a predefined number. I can create the widget and add it to the homescreen just fine, however, when I try to make the widget clickable using setOnClickPendingIntent
, it does not launch the activity. The code I am using is provided below:
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.callwidget_layout);
Log.d("stuff", "remoteview defined");
Intent callIntent = new Intent(Intent.ACTION_CALL);
Log.d("stuff", "intent created");
callIntent.setData(Uri.parse("tel:"+8888888));
Log.d("stuff", "intent data added");
PendingIntent clickPI=PendingIntent
.getBroadcast(context, 0,
callIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
Log.d("stuff", "pending intent created");
remoteViews.setOnClickPendingIntent(R.layout.callwidget_layout, clickPI);
Log.d("stuff", "setonclickpendingintent created");
}
The Log.d
methods work fine, as they show up on the logcat output, but tapping the widget does nothing. There are no other error messages that show up on logcat. Is there anything I am doing wrong?
UPDATE: Changed the setOnClickPendingIntent to reference a button with the ID "callbutton" (remoteViews.setOnClickPendingIntent(R.id.callbutton, clickPI);
), and also tried adding these three lines of code to the onUpdate method:
ComponentName myWidget = new ComponentName(context, WidgetProvider.class);
appWidgetManager.getInstance(context).updateAppWidget(myWidget, remoteViews);
Log.d("stuff", "widget updated");
Again, the Log.d
method works, suggesting that the widgets update fine, but tapping the button still does not do anything.
UPDATE 2: Changing PendingIntent clickPI=PendingIntent.getBroadcast
to PendingIntent clickPI=PendingIntent.getActivity
does not do anything either.
Upvotes: 1
Views: 9972
Reputation: 13367
Use ACTION_DIAL
, not ACTION_CALL
.
See the documentation. E.g. the guide on Common Intents to get the requirements for a system intent, e.g. how to initiate a phone call and the documentation on ACTION_CALL says:
Note: there will be restrictions on which applications can initiate a call; most applications should use the ACTION_DIAL.
Also:
... the Log.d method works, suggesting that the widgets update fine ...
The successful Log.d()
demonstrates that the code got there (a breakpoint would be another way to determine this), but it doesn't give evidence that the widgets got updated successfully. You have a useful hypothesis there, that the widgets got updated and the problem is after that, e.g. the wrong Intent. One way to test that hypothesis would be make your update visibly change the widget, e.g. text in a TextView.
Upvotes: 0
Reputation: 31
Alright, so I have a bit of a workaround to this problem that will have to do until I find an actual solution. This involves launching an activity that in turn launches the intent I want. I initially wanted to avoid this, but oh well.
My new code, for anyone that might need it:
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.callwidget_layout);
Log.d("stuff", "remoteview defined");
Intent callIntent = new Intent(context, CallActivity.class);
PendingIntent clickPI=PendingIntent
.getActivity(context, 0,
callIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
Log.d("stuff", "pending intent created");
remoteViews.setOnClickPendingIntent(R.id.callbutton, clickPI);
Log.d("stuff", "setonclickpendingintent created");
ComponentName myWidget = new ComponentName(context, WidgetProvider.class);
appWidgetManager.getInstance(context).updateAppWidget(myWidget, remoteViews);
Log.d("stuff", "widget updated");
}
CallActivity.java:
package com.example.callzepolice;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
public class CallActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_call);
Intent callIntent = new Intent(Intent.ACTION_CALL);
Log.d("stuff", "intent created");
callIntent.setData(Uri.parse("tel:"+8888888));
Log.d("stuff", "intent data added");
startActivity(callIntent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.call, menu);
return true;
}
}
Upvotes: 2
Reputation: 644
remoteViews.setOnClickPendingIntent(R.layout.callwidget_layout, clickPI);
You should be referencing a view within a layout. Not a layout.
EDIT: If this is the whole method, you are also missing the appWidgetmanager.updateWidgetAppWidget(...)
call.
EDIT 2: You use getBroadcast(...)
when you actually want to broadcast a message to a receiver. For calling activities you need getActivity(...)
.
Upvotes: 1