Issue
So, there are a lot of questions on this but none of the answers have worked and my problem is unique. I am trying to set URI data so I can send that in an intent to a java class from a widget button I have created. I know the widget button is set up correctly because it sends the toast message when clicked.
The problem is: no intent, or variation of intents, I send, after clicking the button, works. It always crashes and the logcat gives a mostly non-descriptive reason why, pointing to the startActivity(i)
. Any help would be much appreciated. Complete code for the widget is below:
package com.matthewbenchimol.cydogbrowser;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RemoteViews;
import android.widget.Toast;
import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
import static androidx.core.content.ContextCompat.startActivity;
import static java.security.AccessController.getContext;
/**
* Implementation of App Widget functionality.
*/
public class Dashboard extends AppWidgetProvider {
private static final String MyOnClick = "myOnClickTag";
void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
// Construct the RemoteViews object
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.dashboard);
Intent intent = new Intent(context, Dashboard.class);
intent.setAction(MyOnClick);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.imageButton, pendingIntent);
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
public void onReceive(Context context, Intent intent){
super.onReceive(context, intent);
String url = "https://test.com";
if (intent.getAction().equals(MyOnClick)) {
Toast.makeText(context.getApplicationContext(), "Under-development", Toast.LENGTH_SHORT).show();
Intent i = new Intent(context, Sandbox.class);
intent.setData(Uri.parse(url));
context.startActivity(i);
}
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// There may be multiple widgets active, so update all of them
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}
@Override
public void onEnabled(Context context) {
// Enter relevant functionality for when the first widget is created
}
@Override
public void onDisabled(Context context) {
// Enter relevant functionality for when the last widget is disabled
}
}
Solution
Not sure the purpose of the broadcast you launch whenever the widget button is clicked; but I believe that the Uri
data should be attached to intent that is associated with the PendingIntent
which will be registered in the system in onUpdate()
callback.
So, if you just really want to send Uri
data to the activity without no specific need to the broadcast receiver; you can just attach it to the intent of the PendingIntent
associated to the button.
public class Dashboard extends AppWidgetProvider {
void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
// Construct the RemoteViews object
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.dashboard);
Intent intent = new Intent(context, Sandbox.class);
String url = "https://test.com";
intent.setData(Uri.parse(url));
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.imageButton, pendingIntent);
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
//....
}
This will send the Uri to the activity:
public class Sandbox extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(....);
Intent intent = getIntent();
if (intent != null) {
Uri data = intent.getData(); // Should contain `https://test.com`
Log.d("LOG_TAG", "onCreate: data: " + data);
} else {
Log.d("LOG_TAG", "onCreate: data: Null");
}
}
}
Answered By - Zain
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.