I need to do a simple widget that presents a Toast when clicking on it.
My problem is that I cant find how to get or create an "oncreate" action. i see the examples with the pending intent that opens the web browser. But how do i simply create this: Toast.makeText(context, "activated", Toast.LENGTH_LONG).show();
and make it happen every time a user clicks on the widget?
just to be clear, I mean a widget on the launcher of the phone. not a regular "button" widget etc...
public class Widget extends AppWidgetProvider {
NotificationManager mNotificationManager;
Notification notification;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
appWidgetManager.updateAppWidget(appWidgetIds, view);
}
}
Thanks!
Just call Toast.makeText(context, "activated", Toast.LENGTH_LONG).show();
in widget's onClick()
method of View.OnClickListener
Update:
If you use AppWidgetProvider
so check this and this one posts
I think you are missing to show the created Toast
. For example:
Toast.makeText(context, "activated", Toast.LENGTH_LONG).show();
Moreover, override the onClick
method of your custom View
class and pop the toast there.
public class TestButton extends Button {
public TestButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
@Override
public void setOnClickListener(OnClickListener l) {
super.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "activated", Toast.LENGTH_LONG)
.show();
}
});
}
}
Button btn=(Button) findViewById(R.id.button);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(context, "activated", Toast.LENGTH_LONG).show();
}
}
});
this may helps , here need to pass context and get Onclick Event with your widget
Toast.makeText(context, "activated", Toast.LENGTH_LONG).show();
Try this:
button = (Button) findViewById(R.id.buttonToast);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Create a piece of toast.
Toast pieceToast = Toast.makeText(getApplicationContext(), "Test Message", Toast.LENGTH_SHORT);
// Show the toast.
pieceToast.show();
}
This is my BroadcastReciever Class:
public class IncomingCallReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if(null == bundle)
return;
Log.i("IncomingCallReceiver",bundle.toString());
String state = bundle.getString(TelephonyManager.EXTRA_STATE);
String registredPhoneNumber;
Log.i("IncomingCallReceiver","State: "+ state);
String phonenumber = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
System.out.println("*****Mobile Ringing*******"+phonenumber);
if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING))
{
registredPhoneNumber = PreferenceConnector.getPhoneNumber(context);
System.out.println("registredPhoneNumber: "+registredPhoneNumber);
System.out.println("phonenumber: "+phonenumber);
Log.i("IncomingCallReceiver","Incomng Number: " + phonenumber);
if(phonenumber.equals(registredPhoneNumber)){
System.out.println("Entered...");
String info = "Detect Calls sample application\nIncoming number: " + phonenumber;
if(isMyServiceRunning(context)){
context.stopService(new Intent(context,com.visiomaticamericas.visitormobile.services.LaunchServiceActivity.class));
System.out.println("******Service Stopped*********");
}
Intent i = new Intent(context,com.services.LaunchServiceActivity.class);
i.putExtra("delay",500L);
context.startService(i);
System.out.println("*****Service Started*****");
Toast.makeText(context, info, Toast.LENGTH_LONG).show();
}
}
}
}
Here, I called my activity class.. and I did my widget design in My activity class..
folks my VS environment is VS 2017 with Xamarin .
private void DisplayMessage(string DisplayMsgText)
{
Toast.MakeText(this.ApplicationContext, DisplayMsgText, ToastLength.Short).Show();
}
and use this function to show message at device screen
like below:
DisplayMessage("Vibrate button pressed");