Creation of button in an Android widget
I am trying to write a code for an SMS widget. I wrote something that I can compile, and print on screen my first SMS. When I click on next, nothing happened. This is my code:
package android.MySMSwidget;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RemoteViews;
import android.content.ComponentName;
import android.content.Context;
import android.database.Cursor;
import android.app.*;
public class MySMSwidget extends AppWidgetProvider implements View.OnClickListener {
private Button Bnext;
private int sms_id=0;
public class MyActivity extends Activity {
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.widget_layout);
final Button button = (Button) findViewById(R.id.next);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (v==Bnext){sms_id=sms_id+1;}
}
});
}
}
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[]开发者_Python百科 appWidgetIds) {
Cursor c = context.getContentResolver().query(Uri.parse("content://sms/"), null, null ,null,null);
String body = null;
String number = null;
String date = null;
c.moveToPosition(sms_id);
body = c.getString(c.getColumnIndexOrThrow("body")).toString();
number = c.getString(c.getColumnIndexOrThrow("address")).toString();
date = c.getString(c.getColumnIndexOrThrow("date")).toString();
c.close();
RemoteViews updateViews = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
updateViews.setTextColor(R.id.text, 0xFF000000);
updateViews.setTextViewText(R.id.text,date+'\n'+number+'\n'+body);
ComponentName thisWidget = new ComponentName(context, MySMSwidget.class);
appWidgetManager.updateAppWidget(thisWidget, updateViews);
}
public void onClick(View v) {
if (v==Bnext){sms_id=sms_id+1;}
}
}
If someone can explain to me where and why I'm wrong, I will be grateful. Thanks for reading me.
Generally it is not possible to add interactive controls to standard Android Widgets. You can detect a click on the Widget itself.
精彩评论