如何显示从活动到窗口小部件的一个整数?

时间:2014-07-01 09:40:23

标签: android android-widget

我必须在我的应用中活动。一个活动用于增加计数值,其他活动扩展AppWidget。因为我需要定期将计数值(整数)显示到app小部件中。

这是我的活动代码。

Smoke_Count.java

package com.example.smokecount;

import android.os.Bundle;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.TextView;

public class Smoke_Count extends Activity implements OnTouchListener{


    private Button btn;
    private TextView txt;
    private static int count=0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_smoke__count);

        btn=(Button)findViewById(R.id.button1);
        txt=(TextView)findViewById(R.id.textView1);
        txt.setText(String.valueOf(count));
        btn.setOnTouchListener(this);
        loadInt();


    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.smoke__count, menu);
        return true;
    }


    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub

        if(event.getAction()==MotionEvent.ACTION_UP)
        {
            loadInt();
            saveInt("countValue", count);
            count++;
            txt.setText(String.valueOf(count));
        }
        return true;
    }

    public void saveInt(String key,int count)
    {
        SharedPreferences share= PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        Editor edit= share.edit();
        edit.putInt(key, count);
        edit.commit();

    }

    public void loadInt()
    {
        SharedPreferences share= PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        int counter=share.getInt("countValue", count);
        /*Intent i=new Intent(Smoke_Count.this, Widget.class);
        i.putExtra("count", counter);
        sendBroadcast(i);*/
        txt.setText(String.valueOf(counter));

        if(counter>=count)
        {
            count=counter;
        }


    }

}

这是我的小部件活动

Widget.java

package com.example.smokecount;

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 Widget extends AppWidgetProvider{



        @Override
        public void onUpdate(Context context, AppWidgetManager appWidgetManager,
                int[] appWidgetIds) {
            // TODO Auto-generated method stub
            super.onUpdate(context, appWidgetManager, appWidgetIds);



            for(int i=0;i<appWidgetIds.length;i++)
            {
                int appWidgetID=appWidgetIds[i];

                Intent i1=new Intent(Intent.ACTION_VIEW, null, context, Smoke_Count.class);
                PendingIntent pending=PendingIntent.getActivity(context, 0, i1, 0);

                RemoteViews views=new RemoteViews(context.getPackageName(), R.layout.activity_widget);
                views.setOnClickPendingIntent(R.id.imageButton1, pending);
                appWidgetManager.updateAppWidget(appWidgetID, views);
            }
        }

        /*@Override
        public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
            super.onReceive(context, intent);
            String action=intent.getAction();
        Bundle ext=intent.getExtras();
        int scount=ext.getInt("count");
        Toast.makeText(context, ""+scount, Toast.LENGTH_LONG).show();
        }*/

}

这是我的widget xml文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/txtcount"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageButton1"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="18dp"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

我需要在小部件中显示计数值。

这是logcat

06-30 18:00:36.950: E/AndroidRuntime(2226): FATAL EXCEPTION: main
06-30 18:00:36.950: E/AndroidRuntime(2226): Process: com.example.smokecount, PID: 2226
06-30 18:00:36.950: E/AndroidRuntime(2226): java.lang.RuntimeException: Unable to start receiver com.example.smokecount.Widget: java.lang.NullPointerException
06-30 18:00:36.950: E/AndroidRuntime(2226):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2407)
06-30 18:00:36.950: E/AndroidRuntime(2226):     at android.app.ActivityThread.access$1600(ActivityThread.java:135)
06-30 18:00:36.950: E/AndroidRuntime(2226):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473)
06-30 18:00:36.950: E/AndroidRuntime(2226):     at android.os.Handler.dispatchMessage(Handler.java:102)
06-30 18:00:36.950: E/AndroidRuntime(2226):     at android.os.Looper.loop(Looper.java:137)
06-30 18:00:36.950: E/AndroidRuntime(2226):     at android.app.ActivityThread.main(ActivityThread.java:4998)
06-30 18:00:36.950: E/AndroidRuntime(2226):     at java.lang.reflect.Method.invokeNative(Native Method)
06-30 18:00:36.950: E/AndroidRuntime(2226):     at java.lang.reflect.Method.invoke(Method.java:515)
06-30 18:00:36.950: E/AndroidRuntime(2226):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
06-30 18:00:36.950: E/AndroidRuntime(2226):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
06-30 18:00:36.950: E/AndroidRuntime(2226):     at dalvik.system.NativeStart.main(Native Method)
06-30 18:00:36.950: E/AndroidRuntime(2226): Caused by: java.lang.NullPointerException
06-30 18:00:36.950: E/AndroidRuntime(2226):     at com.example.smokecount.Widget.onReceive(Widget.java:42)
06-30 18:00:36.950: E/AndroidRuntime(2226):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2400)
06-30 18:00:36.950: E/AndroidRuntime(2226):     ... 10 more

1 个答案:

答案 0 :(得分:0)

评论的那个区域是转移int的部分。所以你需要在Activty中放入Extra(“key”,intValue)并在getIntent()中的Widget中接收int.getExtras()。getInt(“key”)

否则,您可以像以前一样使用SharedPreferences来获取Int。

让我们在活动中说:

Intent i=new Intent(Smoke_Count.this, Widget.class);
i.putExtra("count", counter);

并在Widget中

Bundle bundle = getIntent().getExtras();
int yourInt = bundle.getInt("count", 0);

修改

正如您已经使用过的那样,您应该使用给定的intent而不是getIntent(),这会导致NullpointerException,所以请执行以下操作:

Bundle bundle = intent.getExtras();
int yourInt = bundle.getInt("count", 0);