繁体   English   中英

Android主屏幕小部件,使用按钮调用void功能

[英]Android Homescreen Widget, call void function using a button

我在这件事上停留了2天,在我的小部件活动中,我有这个空缺,我想在小部件中按下按钮时调用它。

我想这样称呼:

 public void sendSMS() {

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

}

按钮的xml布局

<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"

    />

找到了!

这需要在onUpdate方法中

    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);

你需要像这样覆盖onRecive

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

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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