بروز خطا
   [message]
اشتراک در سوال
رای ها
[dataList]
پرسش و پاسخ

شروع اکتیویتی جدید با کلیک روی دکمه در ویجت

مدیریت گفتگو
psycho 11 سال پیش
آخرین تغییر: 11 سال پیش

سلام دوستان

میخوام تو ویجت برنامم چند تا دکمه بذارم که کاربر هر کدوم رو لمس میکنه یه اکتیویتی باز بشه

هرچی گشتم چه تو سایتای ایرانی چه خارجی به نتیجه نرسیدم و فقط گیج شدم ! :(

ممنون میشم یکی راهنماییم کنه :)

نظرات

Leon 11 سال پیش
اینجا رو بخونید شاید حل بشه :
psycho 11 سال پیش
ممنون! :) البته من میخوام 4تا دکمه بذارم که با کلیک روی هرکدوم یه اکتیویتی جدید باز بشه نه اینکه با کلیک روی خود ویجت اکتیویتی جدیدی باز بشه
Leon 11 سال پیش
نمیشه کدهایی که اونجا گفته رو به رویداد تاچ هر دکمه نسبت داد با نام کلاس دلخواه اکتیویتی دلخواه رو باز کنه ؟
psycho 11 سال پیش
مشکل اینجاست که اصلا از کدش سر در نمیارم که اگه بتونم بفهمم کدوم قسمتشو کپی کنم و چه تغییراتی بدم! :(

پاسخ‌ها 3

psycho 11 سال پیش

یعنی یه نفر پیدا نمیشه جواب بده ؟!
تا حالا ویجت نساختین توش دکمه باشه بخواین یه اکتیویتی جدیدو باز کنین؟

هیچ جاااااااااااااااااااااااا آموزشش نیست :((((((((((((((((

نظرات

atf 11 سال پیش

کلاس ویجت (در این جا وقتی رو ویجت زده میشه به کلاس ActivityMain وارد میشه )

 
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;

public class ExampleAppWidgetProvider extends AppWidgetProvider {

    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        final int N = appWidgetIds.length;

        // Perform this loop procedure for each App Widget that belongs to this provider
        for (int i=0; i<N; i++) {
            int appWidgetId = appWidgetIds[i];
اکتیویتی که کاربر وقتی کلیک کنه اجرا میشه
// Create an Intent to launch ExampleActivity
Intent intent = new Intent(context, ActivityMain.class); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); // Get the layout for the App Widget and attach an on-click listener // to the button RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.example_appwidget); views.setOnClickPendingIntent(R.id.analogClock1, pendingIntent); // Tell the AppWidgetManager to perform an update on the current app widget appWidgetManager.updateAppWidget(appWidgetId, views); } } }

لی اوت ویجت ( بجای ساعتی که من گذاشتم هر چیزی میتونه باشه )

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <AnalogClock
        android:id="@+id/analogClock1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:dial="@drawable/backclock"
        android:resizeMode="horizontal|vertical" />

</LinearLayout>

xml ویجت ( 144 در 144 اندازه ویجت هست که می تونید تغییر بدید ) این برای یک ویجت 2*2

 <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:initialLayout="@layout/example_appwidget"
    android:minHeight="144dp"
    android:minWidth="144dp"
    android:updatePeriodMillis="86400000" >

</appwidget-provider>

این قسمت رو هم در مانیفست در تگ اپلیکیشن اضافه کنید

 <receiver
            android:name="ExampleAppWidgetProvider"
            android:label="clock Widget" >
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>

            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/example_appwidget_info" />
        </receiver>

( تو label اسم ویجت تونه )

نظرات

f-Dehghan 11 سال پیش

ببینید این به دردتون میخوره این هم لینکش

 publicclassExampleProviderextendsAppWidgetProvider{// our actions for our buttonspublicstaticString ACTION_WIDGET_REFRESH ="ActionReceiverRefresh";publicstaticString ACTION_WIDGET_SETTINGS ="ActionReceiverSettings";publicstaticString ACTION_WIDGET_ABOUT ="ActionReceiverAbout";@Overridepublicvoid onUpdate(Context context,AppWidgetManager appWidgetManager,int[] appWidgetIds){RemoteViews remoteViews =newRemoteViews(context.getPackageName(), R.layout.main_layout);Intent active =newIntent(context,ExampleProvider.class);
    active.setAction(ACTION_WIDGET_REFRESH);PendingIntent actionPendingIntent =PendingIntent.getBroadcast(context,0, active,0);
    remoteViews.setOnClickPendingIntent(R.id.button_refresh, actionPendingIntent);

    active =newIntent(context,ExampleProvider.class);
    active.setAction(ACTION_WIDGET_SETTINGS);
    actionPendingIntent =PendingIntent.getBroadcast(context,0, active,0);
    remoteViews.setOnClickPendingIntent(R.id.button_settings, actionPendingIntent);

    active =newIntent(context,ExampleProvider.class);
    active.setAction(ACTION_WIDGET_ABOUT);
    actionPendingIntent =PendingIntent.getBroadcast(context,0, active,0);
    remoteViews.setOnClickPendingIntent(R.id.button_about, actionPendingIntent);

    appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);}@Overridepublicvoid onReceive(Context context,Intent intent){if(intent.getAction().equals(ACTION_WIDGET_REFRESH)){Log.i("onReceive", ACTION_WIDGET_REFRESH);}elseif(intent.getAction().equals(ACTION_WIDGET_SETTINGS)){Log.i("onReceive", ACTION_WIDGET_SETTINGS);}elseif(intent.getAction().equals(ACTION_WIDGET_ABOUT)){Log.i("onReceive", ACTION_WIDGET_ABOUT);}else{super.onReceive(context, intent);}}...

//manifest
<receiverandroid:name="ExampleProvider"><intent-filter><actionandroid:name="android.appwidget.action.APPWIDGET_UPDATE"/><actionandroid:name="org.divenvrsk.widgets.ExampleProvider.ACTION_WIDGET_REFRESH"/><actionandroid:name="org.divenvrsk.widgets.ExampleProvider.ACTION_WIDGET_SETTINGS"/><actionandroid:name="org.divenvrsk.widgets.ExampleProvider.ACTION_WIDGET_ABOUT"/></intent-filter><meta-dataandroid:name="android.appwidget.provider"android:resource="@xml/widget_info"/></receiver></application>

نظرات


پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .