[英]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.