Несколько кликов на AppWidget
Я разрабатываю 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();
}
}