1

私は、3 つの異なるシステム操作のための 3 つのボタンを持つ単純なウィジェットを持っています。

xml フォルダーに XML を作成しました。コードは以下のとおりです。

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

私のレイアウトファイルは以下です

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="80dp"
android:gravity="top"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:background="#D8F781"
>
<Button
  android:id="@+id/button1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentLeft="true"
  android:layout_alignParentTop="true"
  android:text="Off" />
  <Button android:id="@+id/button2"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_toRightOf="@+id/button1"
  android:text="off"/>

 <Button android:id="@+id/button3"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_toRightOf="@+id/button2"
 android:text="off"/>   
</RelativeLayout>

これは、AppWidgetProvider を拡張する MainActivity のコードです。

public class MainActivity extends AppWidgetProvider{
Context ctx;
private static final String XYZ    = "xyz";
private static final String ABC    = "abc";
private static final String CFV    = "cfv";


@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
  ctx = context;
  for(int i=0; i<appWidgetIds.length; i++){
  int currentWidgetId = appWidgetIds[i];
  RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.activity_main);     

  views.setOnClickPendingIntent(R.id.button2, getPendingSelfIntent(context, XYZ));
  views.setOnClickPendingIntent(R.id.button1, getPendingSelfIntent(context, ABC));
  views.setOnClickPendingIntent(R.id.button3, getPendingSelfIntent(context, CFV));
  appWidgetManager.updateAppWidget(currentWidgetId,views);      
  Toast.makeText(context, "widget used", Toast.LENGTH_SHORT).show();    
  }
 }

protected PendingIntent getPendingSelfIntent(Context context, String action) {
   Log.v("ONMESSAGE", action);
   Intent intent = new Intent(context, getClass());
   intent.setAction(action);
   return PendingIntent.getBroadcast(context, 0, intent, 0);
}
@Override
public void onReceive(Context context, Intent intent) {
   // TODO Auto-generated method stub
   super.onReceive(context, intent);
   ctx = context;
   final WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
   RemoteViews remoteViews;
   AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);       
   ComponentName watchWidget;
   remoteViews = new RemoteViews(context.getPackageName(), R.layout.activity_main);
   if (XYZ.equals(intent.getAction())) {  
       //Logic for button 2
   }
   else if(ABC.equals(intent.getAction()))
   {      
      //Logic for button 1
   }
   else if(AUTO_ROTATE.equals(intent.getAction()))
   {      
      //Logic for button 3
   }


   watchWidget = new ComponentName( context, MainActivity.class );
   (AppWidgetManager.getInstance(context)).updateAppWidget( watchWidget, remoteViews );
 }
 @Override
public void onEnabled(Context context) {
    // TODO Auto-generated method stub
    super.onEnabled(context);
    Log.v("ONMESSAGE", "Working");
}

ここにマニフェストがあります

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.afixi.tetheringwidget"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <receiver android:name="MainActivity" >
        <intent-filter>
           <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>
        <meta-data android:name="android.appwidget.provider"
           android:resource="@xml/main" />
     </receiver>
</application>

最初にインストールしたときはすべて機能していましたが、コードを変更して再インストールした後、機能しなくなりました。初めてウィジェットを作っています。

N:B:- 新しい携帯電話にインストールしようとしたところ、動作するようになりました。しばらくすると、更新されないか、ウィジェットからの保留中のインテントの受信が停止します。

4

0 に答える 0