我正在开发一个AppWidget,当用户点击它时会打开我的活动4次。截至目前,我的代码在用户点击一次时起作用。有没有办法让appWidget在启动我的活动之前点击4次?
这是我的Widget.java:
public class Widget extends AppWidgetProvider{
public static String ACTION_WIDGET_CONFIGURE = "ConfigureWidget";
public static String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget";
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
final String action = intent.getAction();
if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)) {
String msg = "null";
try {
msg = intent.getStringExtra("msg");
} catch (NullPointerException e) {
Log.e("Error", "msg = null");
}
intent.setClassName("com.kerrigan.itproj", "com.kerrigan.itproj.NotAutoSOSAlert");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, 0);
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification noty = new Notification(R.drawable.emergency, "Emergency Alert Sent!", System.currentTimeMillis());
noty.setLatestEventInfo(context, "Notice", msg, contentIntent);
notificationManager.notify(1, noty);
}
super.onReceive(context, intent);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// TODO Auto-generated method stub
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
Intent active = new Intent(context,Widget.class);
active.setAction(ACTION_WIDGET_RECEIVER);
active.putExtra("msg", "Emergency Alert Sent!");
PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
remoteViews.setOnClickPendingIntent(R.id.ibEmergency, actionPendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
} }
这是我的AndroidManifest:
<receiver android:name=".Widget" android:label="@string/app_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<!-- Broadcast Receiver that will also process our self created action -->
<action android:name="de.thesmile.android.widget.buttons.ButtonWidget.ACTION_WIDGET_RECEIVER"/>
</intent-filter>
<meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_provider" />
</receiver>
widget.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_gravity="center"
android:layout_height="wrap_content">
<ImageButton
android:id="@+id/ibEmergency"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/red_button" />
</LinearLayout>
widget_provider.xml:
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="146dip"
android:minHeight="72dip"
android:updatePeriodMillis="1"
android:initialLayout="@layout/widget"
/>
有可能这样做吗?因为我试图创建一个全局变量,然后递增它并创建一个if语句,但它没有工作:(帮帮我们!谢谢。
这是我迄今为止尝试使用共享偏好
的内容public class Widget extends AppWidgetProvider{
public static String ACTION_WIDGET_CONFIGURE = "ConfigureWidget";
public static String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget";
int count = 0;
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
final String action = intent.getAction();
if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)) {
String msg = "null";
try {
msg = intent.getStringExtra("msg");
} catch (NullPointerException e) {
Log.e("Error", "msg = null");
}
Log.d("receiver", "receive");
SharedPreferences prefs1 = context.getSharedPreferences("widget", 0);
int i = prefs1.getInt("count", 0);
count += i;
Toast.makeText(context, String.valueOf(count), Toast.LENGTH_LONG).show();
if(count > 4) {
intent.setClassName("com.kerrigan.itproj", "com.kerrigan.itproj.NotAutoSOSAlert");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
}
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, 0);
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification noty = new Notification(R.drawable.emergency, "Emergency Alert Sent!", System.currentTimeMillis());
noty.setLatestEventInfo(context, "Notice", msg, contentIntent);
notificationManager.notify(1, noty);
}
super.onReceive(context, intent);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// TODO Auto-generated method stub
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
Intent active = new Intent(context,Widget.class);
active.setAction(ACTION_WIDGET_RECEIVER);
active.putExtra("msg", "Emergency Alert Sent!");
Intent passive = new Intent(context,Widget.class);
passive.setAction(ACTION_WIDGET_RECEIVER);
//when you will click button1 the message "Message for Button 1" will appear as a notification
//you can do whatever you want anyway on the press of this button
PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
PendingIntent actPendingIntent = PendingIntent.getBroadcast(context, 0, passive, PendingIntent.FLAG_UPDATE_CURRENT );
if(count <= 5) {
remoteViews.setOnClickPendingIntent(R.id.ibEmergency, actPendingIntent);
Log.d("act", "act");
}
if(count >= 5) {
remoteViews.setOnClickPendingIntent(R.id.ibEmergency, actionPendingIntent);
Log.d("action", "action");
}
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
count++;
SharedPreferences prefs = context.getSharedPreferences("widget", 0);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt("count", count);
editor.commit();
}
}