简体   繁体   中英

Android Homescreen Widget, call void function using a button

Im stuck 2 days on this thing, in my widget activity i have this void which i want to call when a button is pressed in the widget.

i want to call this:

 public void sendSMS() {

    smsManager.sendTextMessage("0123456789", null, SMSTEXT, null, null);

}

the xml layout of the button

<Button
    android:text="Button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/send_button"
    android:layout_marginStart="54dp"
    android:layout_centerVertical="true"
    android:layout_alignStart="@+id/appwidget_text"

    />

Found it!

This needs to be in the onUpdate method

    Intent intent = new Intent(context, ParkingWidget.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

    RemoteViews newviews = new RemoteViews(context.getPackageName(), R.layout.parking_widget);
    newviews.setOnClickPendingIntent(R.id.send_button, pendingIntent);
    appWidgetManager.updateAppWidget(appWidgetIds[0], newviews);

and you need to overide onRecive like this

@Override
  public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);
    sendSMS();  // code goes here

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM