[Q] AppWidget Creation Intents - Android Software Development

i was struggling with creating an onClickListener for an Appwidget so instead of setting it in onUpdate i was doing it in one of the later intents in onRecieve. are the Intents used to create AppWidgets the same for all android phones? cause they have some pretty dubious names for CM7 on my Inspire 4G? can anyone confirm that these are the same on another build of Android?
I/GITextCloud(32189): android.appwidget.action.APPWIDGET_ENABLED
I/GITextCloud(32189): android.appwidget.action.APPWIDGET_UPDATE
I/GITextCloud(32189): com.motorola.blur.home.ACTION_SET_WIDGET_SIZE
I/GITextCloud(32189): mobi.intuitit.android.hpp.ACTION_READY
are these the same for every build of android? i am using CM7 on an Inspire 4G so why is a motorola blur intent being sent? or an intuitit intent? can i rely on these being there for all builds of android? not just CM7?
---------------------
i rambled in my original post. it was quite insane. it is in the second post.

I have had a rather strange problem with one of my AppWidgets i am writing. i have it so when the widget is clicked gmail opens. the problem arrises from when the setOnClickListener is run.
normally one would recommend setting your onClickListeners in the onUpdate() method as it is the easiest since all the info you need is passed to it. but when it is like this the onClickListener is set before the widget exists on the screen and thus the onClickListener doesnt attach. clicking the widget does nothing.
Code:
/** Called when the activity is first created. */
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
//attach an onClick intent to the layout
final Intent onClick = new Intent(context, GITextCloud.class);
onClick.setAction(LAUNCH_GMAIL_GAPPS);
PendingIntent onClickPending = PendingIntent.getBroadcast(context, 0, onClick, 0);
RemoteViews rv1 = new RemoteViews(context.getPackageName(), R.layout.gitc_widget_html);
rv1.setOnClickPendingIntent(R.id.full_widget, onClickPending);
for (int appWidgetId : appWidgetIds) {
appWidgetManager.updateAppWidget(appWidgetId, rv1);
}
}
so i decided i would perform my setOnClickListener action after returning from WidgetConfig Activity. upon exiting the Config i sent a delayed broadcast to my widget which i would catch in the onRecieve method like so
Code:
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
// other intent catches....
if (intent.getAction().equals(SETUP_DELAY)) {
Toast.makeText(context, "setup delay over", Toast.LENGTH_LONG).show();
//attach an onClick intent to the layout
final Intent onClick = new Intent(context, GITextCloud.class);
onClick.setAction(LAUNCH_GMAIL_GAPPS);
PendingIntent onClickPending = PendingIntent.getBroadcast(context, 0, onClick, 0);
RemoteViews rv1 = new RemoteViews(context.getPackageName(), R.layout.gitc_widget_html);
rv1.setOnClickPendingIntent(R.id.full_widget, onClickPending);
AppWidgetManager manager = AppWidgetManager.getInstance(context);
int[] ids = manager.getAppWidgetIds(new ComponentName(context, GITextCloud.class));
for (int id : ids) {
manager.updateAppWidget(id, rv1);
}
}
// other intent catches....
}
now that works great as long as you press ok on the AppWidget size confirmation before the time on the delayed broadcast from my Config Activity runs out.
so i decided i would look at what intents the appwidget was receiving and perform the setOnClickListener at the last intent received.
they are as follows...
I/GITextCloud(32189): android.appwidget.action.APPWIDGET_ENABLED
I/GITextCloud(32189): android.appwidget.action.APPWIDGET_UPDATE
I/GITextCloud(32189): com.motorola.blur.home.ACTION_SET_WIDGET_SIZE
I/GITextCloud(32189): mobi.intuitit.android.hpp.ACTION_READY
are these the same for every build of android? i am using CM7 on an Inspire 4G so why is a motorola blur intent being sent? or an intuitit intent? can i rely on these being there for all builds of android? not just CM7?
Click to expand...
Click to collapse
rambling and quite unnecessary

are we devoid of any knowledge of this?

Related

Finish Appwidget Config Activity

i have an Appwidget that i have been working on in conjuntion with an Appwidget Config Activity that runs before the Appwidget gets placed. i have two buttons in the config activity. one for canceling out of creating the widget and one for making the widget. i am fine on the cancel button, but no matter what i do for my create button i cant seem to get it to make the appwidget.
how can i have a button create my appwidget inside my appwidget config activity?
----
ANSWER IN #3
Bump.
It would be logical to have finish() make the widget appear but it does not. Really need some help / direction with this
From something awesome
hope this helps others who are looking to make this happen.
Code:
private int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setResult(RESULT_CANCELED);
setContentView(R.layout.gitc_config);
findViewById(R.id.create_button).setOnClickListener(mOnClickListener);
// Find the widget id from the intent.
Intent intent = getIntent();
Bundle extras = intent.getExtras();
if (extras != null) {
mAppWidgetId = extras.getInt(
AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
}
// If they gave us an intent without the widget id, just bail.
if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
finish();
}
}
View.OnClickListener mOnClickListener = new View.OnClickListener() {
public void onClick(View v) {
// Make sure we pass back the original appWidgetId
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
setResult(RESULT_OK, resultValue);
finish();
}
};

[Q] Dialog out of Activity

Hello app developers!
I have got serious problem. Every time I call alertdialogs or other dialogs I see them only in activity. But my app needs to show this dialog when user works on phone, that's why it must be shown out of activity. Any ideas?
DoR2 said:
Hello app developers!
I have got serious problem. Every time I call alertdialogs or other dialogs I see them only in activity. But my app needs to show this dialog when user works on phone, that's why it must be shown out of activity. Any ideas?
Click to expand...
Click to collapse
You cannot do this. There is simply no feature for that. I have had that problem, too.
Use a Notification or a Toast message.
There is a work-around for this. If you run a service, you can technically launch an activity that can resemble this.
I have an app that uses a broadcast receiver to listen for the events I want to alert my user to. I created a custom layout for my alert dialog and then when the event happens, my broadcast receiver calls the activity.
zalez said:
There is a work-around for this. If you run a service, you can technically launch an activity that can resemble this.
I have an app that uses a broadcast receiver to listen for the events I want to alert my user to. I created a custom layout for my alert dialog and then when the event happens, my broadcast receiver calls the activity.
Click to expand...
Click to collapse
Yes, great idea. Launch a transparent Activity and start a dialog. Set an onDismissListener and close the app when the dialog is closed.
nikwen said:
Yes, great idea. Launch a transparent Activity and start a dialog. Set an onDismissListener and close the app when the dialog is closed.
Click to expand...
Click to collapse
If I start a transparent activity user can't see that it was started as I understand. Can you give code for broadcast receiver maybe I misunderstand something
The broadcast receiver is just the mechanism I use to launch the activity. It is a mere intent that I start from it. Below is the class I use for my alertdialog. You won't be able to cut and paste because I also created my own class to dismiss the activity.
Code:
public class SilentDialog extends TimedActivity implements OnTouchListener{
Intent intent;
[user=439709]@override[/user]
public void onCreate(Bundle savedInstanceState) {
//Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//Remove notification bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
showAlert();
}
[user=439709]@override[/user]
protected void onDestroy()
{
//this is very important here ;)
super.onDestroy();
}
public boolean onTouch(View v, MotionEvent event)
{
final int actionPerformed = event.getAction();
//reset idle timer
// put this here so that the touching of empty space is captured too
// it seems that LinearLayout doesn't trigger a MotionEvent.ACTION_UP or MotionEvent.ACTION_MOVE
if (actionPerformed == MotionEvent.ACTION_DOWN)
{
super.onTouch();
}
return false;//do not consume event!
}
public void showAlert(){
//would you like it to expire?
AlertDialog.Builder builder;
final AlertDialog alertDialog;
Context mContext = SilentDialog.this;
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.pop1,
(ViewGroup) findViewById(R.id.layout_root));
NumberPicker spin = (NumberPicker) layout.findViewById(R.id.SpinRate);
spin.setVisibility(8);
TextView rate = (TextView) layout.findViewById(R.id.RateTitle);
rate.setVisibility(8);
TextView text = (TextView) layout.findViewById(R.id.txtAlertDiag);
text.setText("ButlerSMS has detected the ringer mode has changed to silent. " +
"\n\n Would you like ButlerSMS to turn on?");
final NumberPicker picker = (NumberPicker) layout.findViewById(R.id.SpinRate);
picker.setValue(60);
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
builder.setTitle("ButlerSMS - Silent Mode");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
intent = new Intent(getBaseContext(), ButlerWidget.class);
intent.setAction("StartSMS");
intent.putExtra("msg","Normal");
sendBroadcast(intent);
finish();
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
finish();
}
});
alertDialog = builder.create();
alertDialog.show();
final Timer t = new Timer();
t.schedule(new TimerTask() {
public void run() {
alertDialog.dismiss(); // when the task is active then close the dialog
t.cancel(); // also just top the timer thread, otherwise, you may receive a crash report
finish();
}
}, 19000);
}
}
zalez said:
The broadcast receiver is just the mechanism I use to launch the activity. It is a mere intent that I start from it. Below is the class I use for my alertdialog. You won't be able to cut and paste because I also created my own class to dismiss the activity.
Code:
public class SilentDialog extends TimedActivity implements OnTouchListener{
Intent intent;
[user=439709]@override[/user]
public void onCreate(Bundle savedInstanceState) {
//Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//Remove notification bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
showAlert();
}
[user=439709]@override[/user]
protected void onDestroy()
{
//this is very important here ;)
super.onDestroy();
}
public boolean onTouch(View v, MotionEvent event)
{
final int actionPerformed = event.getAction();
//reset idle timer
// put this here so that the touching of empty space is captured too
// it seems that LinearLayout doesn't trigger a MotionEvent.ACTION_UP or MotionEvent.ACTION_MOVE
if (actionPerformed == MotionEvent.ACTION_DOWN)
{
super.onTouch();
}
return false;//do not consume event!
}
public void showAlert(){
//would you like it to expire?
AlertDialog.Builder builder;
final AlertDialog alertDialog;
Context mContext = SilentDialog.this;
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.pop1,
(ViewGroup) findViewById(R.id.layout_root));
NumberPicker spin = (NumberPicker) layout.findViewById(R.id.SpinRate);
spin.setVisibility(8);
TextView rate = (TextView) layout.findViewById(R.id.RateTitle);
rate.setVisibility(8);
TextView text = (TextView) layout.findViewById(R.id.txtAlertDiag);
text.setText("ButlerSMS has detected the ringer mode has changed to silent. " +
"\n\n Would you like ButlerSMS to turn on?");
final NumberPicker picker = (NumberPicker) layout.findViewById(R.id.SpinRate);
picker.setValue(60);
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
builder.setTitle("ButlerSMS - Silent Mode");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
intent = new Intent(getBaseContext(), ButlerWidget.class);
intent.setAction("StartSMS");
intent.putExtra("msg","Normal");
sendBroadcast(intent);
finish();
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
finish();
}
});
alertDialog = builder.create();
alertDialog.show();
final Timer t = new Timer();
t.schedule(new TimerTask() {
public void run() {
alertDialog.dismiss(); // when the task is active then close the dialog
t.cancel(); // also just top the timer thread, otherwise, you may receive a crash report
finish();
}
}, 19000);
}
}
Click to expand...
Click to collapse
I understood everything except the way how app switch between position before signal came and required activity with alert dialog in it. As I see this code initialize activity and alert dialog but don't contain switching that I need
Are you asking how I call the dialog? If so, a simple intent from a broadcast receiver.
Code:
Intent i = new Intent(context, SilentDialog.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
I would use the AlertDialog.Builder class for compatibility.
Why not use a notification, I think that is more elegant.
Code:
public class InstalledReceiver extends BroadcastReceiver {
private NotificationManager mNotificationManager ;
[user=439709]@override[/user]
public void onReceive(Context context, Intent intent) {
if (BaseActivity.DEBUG) System.out.println("Received Broadcast");
Boolean update = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
mNotificationManager = (NotificationManager) context.getSystemService("notification");
SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(context);
Boolean disabledNotifications = getPrefs.getBoolean("disableNotifications", false);
if (!disabledNotifications && !update) makeNotification(context);
}
private void makeNotification(Context context) {
CharSequence label = context.getString(R.string.labelNotify);
CharSequence text = context.getString(R.string.textNotify);
CharSequence full = context.getString(R.string.fullNotify);
final Notification notification = new Notification(R.drawable.ic_launcher,text,System.currentTimeMillis());
notification.setLatestEventInfo(context,label,full,null);
notification.defaults = Notification.DEFAULT_ALL;
mNotificationManager.notify( 0, notification);
}
}
Taptalked u see .. əəs n pəʞlɐʇdɐʇ
Zatta said:
Why not use a notification, I think that is more elegant.
Code:
public class InstalledReceiver extends BroadcastReceiver {
private NotificationManager mNotificationManager ;
[user=439709]@override[/user]
public void onReceive(Context context, Intent intent) {
if (BaseActivity.DEBUG) System.out.println("Received Broadcast");
Boolean update = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
mNotificationManager = (NotificationManager) context.getSystemService("notification");
SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(context);
Boolean disabledNotifications = getPrefs.getBoolean("disableNotifications", false);
if (!disabledNotifications && !update) makeNotification(context);
}
private void makeNotification(Context context) {
CharSequence label = context.getString(R.string.labelNotify);
CharSequence text = context.getString(R.string.textNotify);
CharSequence full = context.getString(R.string.fullNotify);
final Notification notification = new Notification(R.drawable.ic_launcher,text,System.currentTimeMillis());
notification.setLatestEventInfo(context,label,full,null);
notification.defaults = Notification.DEFAULT_ALL;
mNotificationManager.notify( 0, notification);
}
}
Taptalked u see .. əəs n pəʞlɐʇdɐʇ
Click to expand...
Click to collapse
I agree, but if he wants to use a dialog, we help him to make one.
The bad thing about the dialog is that it will interrupt whatever the user is doing. This could be very annoying. And who wants to use an app which has annoying popups?
zalez said:
Are you asking how I call the dialog? If so, a simple intent from a broadcast receiver.
Code:
Intent i = new Intent(context, SilentDialog.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
Click to expand...
Click to collapse
When I use this code activity don't shows when I am out of application. For example Handcent SMS when SMS comes to user shows up a great dialog over all windows. My aim is the same thing
DoR2 said:
When I use this code activity don't shows when I am out of application. For example Handcent SMS when SMS comes to user shows up a great dialog over all windows. My aim is the same thing
Click to expand...
Click to collapse
Start an Activity. Set a transparent layout. Show a dialog. That will result in what you want.
nikwen said:
Start an Activity. Set a transparent layout. Show a dialog. That will result in what you want.
Click to expand...
Click to collapse
I have used this code
Code:
Intent i = new Intent(context, SilentDialog.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
and made transparent layout with dialog, but my dialog appears only in my app
DoR2 said:
I have used this code
Code:
Intent i = new Intent(context, SilentDialog.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
and made transparent layout with dialog, but my dialog appears only in my app
Click to expand...
Click to collapse
Could you please post your code?
nikwen said:
Could you please post your code?
Click to expand...
Click to collapse
How I call activity
Code:
if(answer.contains("BEEP")){
Intent intent=new Intent();
intent.setAction("Navi_Beep");
sendBroadcast(intent);
r.play();
Intent i = new Intent(context, NBeep.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
NBeep.java
Code:
public class NBeep extends Activity {
[user=439709]@override[/user]
public void onCreate(Bundle savedInstanceState) {
Log.d("NaviBeep","Here");
//super.onCreate(savedInstanceState);
super.onCreate(savedInstanceState);
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Title");
alertDialog.setMessage("Message");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent=new Intent();
intent.setAction("Navi_BeepOff");
sendBroadcast(intent);
finish();
}
});
// Set the Icon for the Dialog
alertDialog.show();
}
}
DoR2 said:
How I call activity
Code:
if(answer.contains("BEEP")){
Intent intent=new Intent();
intent.setAction("Navi_Beep");
sendBroadcast(intent);
r.play();
Intent i = new Intent(context, NBeep.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
NBeep.java
Code:
public class NBeep extends Activity {
[user=439709]@override[/user]
public void onCreate(Bundle savedInstanceState) {
Log.d("NaviBeep","Here");
//super.onCreate(savedInstanceState);
super.onCreate(savedInstanceState);
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Title");
alertDialog.setMessage("Message");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent=new Intent();
intent.setAction("Navi_BeepOff");
sendBroadcast(intent);
finish();
}
});
// Set the Icon for the Dialog
alertDialog.show();
}
}
Click to expand...
Click to collapse
Ah. You need to call setContentView. Create a transparent View and pass it as a parameter.
nikwen said:
Ah. You need to call setContentView. Create a transparent View and pass it as a parameter.
Click to expand...
Click to collapse
Doesn't help:
Code:
public class NBeep extends Activity {
[user=439709]@override[/user]
public void onCreate(Bundle savedInstanceState) {
Log.d("NaviBeep","Here");
View view=new View(this);
view.setBackgroundColor(Color.TRANSPARENT);
setContentView(view);
super.onCreate(savedInstanceState);
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Title");
alertDialog.setMessage("Message");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent=new Intent();
intent.setAction("Navi_BeepOff");
sendBroadcast(intent);
finish();
}
});
// Set the Icon for the Dialog
alertDialog.show();
}
}
If you use another layout, is the Activity opened?
Is the "Here" written to the log? Is there any Error message?
And I recommend configuring the AlertDialog within the AlertDialog.Builder: http://www.mkyong.com/android/android-alert-dialog-example/
(However, I guess that it will not solve your problem.)
nikwen said:
If you use another layout, is the Activity opened?
Is the "Here" written to the log? Is there any Error message?
And I recommend configuring the AlertDialog within the AlertDialog.Builder: http://www.mkyong.com/android/android-alert-dialog-example/
(However, I guess that it will not solve your problem.)
Click to expand...
Click to collapse
I finally made it!:victory: Here is code:
1) Call dialog:
Code:
NBeep.createDialog(NBeep.DIALOG_ERROR, context);
2) NBeep.java
Code:
public class NBeep extends Activity{
public final static int DIALOG_ERROR = 4;
protected Dialog onCreateDialog(int id) {
Dialog dialog;
switch(id) {
case DIALOG_ERROR:
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Title");
alertDialog.setMessage("Message");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent=new Intent();
intent.setAction("Navi_BeepOff");
sendBroadcast(intent);
finish();
}
});
alertDialog.setCancelable(false);
dialog = alertDialog;//new AlertDialog.Builder(this).setMessage("ERROR! This is a global dialog\n Brought to you by Sherif").create();
break;
default:
dialog = null;
}
return dialog;
}
[user=439709]@override[/user]
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
showDialog(DIALOG_ERROR);
}
public static void createDialog(int dialog, Context context){
Intent myIntent = new Intent(context, NBeep.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
}
}

Alarm Intent not triggered

Hello,
I am developing an app related to alarm service and facing an issue of Intent not being called.
I have registered for event in manifest file:
<receiver android:name="com.example.myfirstapp.EventTriggerManager" >
</receiver>
And have implemented EventTriggerManager class extended from BroadcastReceiver:
public class EventTriggerManager extends BroadcastReceiver
{
@override
public void onReceive(Context context, Intent intent)
{
System.out.println("Alarm raised");
Toast.makeText(context, "Don't panik You have an alarm!!!!.", Toast.LENGTH_LONG).show();
}
}
Adding an alarm from the Activity class:
Intent intent = new Intent(this, EventTriggerManager.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 12345678, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, totalTime, pendingIntent);
But alarm is not getting triggered when totalTime is reached in the system.
Note: totalTime is absolute time used.
Pls point my mistake why the expected output is not seen.
Regards,
Sunil
Sunil K said:
Hello,
I am developing an app related to alarm service and facing an issue of Intent not being called.
I have registered for event in manifest file:
<receiver android:name="com.example.myfirstapp.EventTriggerManager" >
</receiver>
And have implemented EventTriggerManager class extended from BroadcastReceiver:
public class EventTriggerManager extends BroadcastReceiver
{
@override
public void onReceive(Context context, Intent intent)
{
System.out.println("Alarm raised");
Toast.makeText(context, "Don't panik You have an alarm!!!!.", Toast.LENGTH_LONG).show();
}
}
Adding an alarm from the Activity class:
Intent intent = new Intent(this, EventTriggerManager.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 12345678, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, totalTime, pendingIntent);
But alarm is not getting triggered when totalTime is reached in the system.
Note: totalTime is absolute time used.
Pls point my mistake why the expected output is not seen.
Regards,
Sunil
Click to expand...
Click to collapse
use this one :
Code:
Intent myIntent = new Intent(Current.this , NotifyService.class);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
pendingIntent = PendingIntent.getService(ThisApp.this, 0, myIntent, 0);
insted of :
Code:
Intent intent = new Intent(this, EventTriggerManager.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 12345678, intent, 0);
mohamedrashad said:
use this one :
Code:
Intent myIntent = new Intent(Current.this , NotifyService.class);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
pendingIntent = PendingIntent.getService(ThisApp.this, 0, myIntent, 0);
insted of :
Code:
Intent intent = new Intent(this, EventTriggerManager.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 12345678, intent, 0);
Click to expand...
Click to collapse
My EventTriggerManager extends BroadcastReceiver and it is not service right? Correct me if wrong.
I tried with the above changes but facing error:
04-19 21:25:00.019: W/ActivityManager(885): Unable to start service Intent { flg=0x4 cmp=com.example.myfirstapp/.EventTriggerManager (has extras) }: not found
Sunil K said:
My EventTriggerManager extends BroadcastReceiver and it is not service right? Correct me if wrong.
I tried with the above changes but facing error:
04-19 21:25:00.019: W/ActivityManager(885): Unable to start service Intent { flg=0x4 cmp=com.example.myfirstapp/.EventTriggerManager (has extras) }: not found
Click to expand...
Click to collapse
sorry I copied this from an old app I made, change get service => get broadcast and the notify.class to your reciver
mohamedrashad said:
sorry I copied this from an old app I made, change get service => get broadcast and the notify.class to your reciver
Click to expand...
Click to collapse
That is what I had done right:
Intent intent = new Intent(this, EventTriggerManager.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 12345678, intent, 0);
Here eventTriggerManager is my receiver class. But alarm is not recevied by application
Sunil K said:
That is what I had done right:
Intent intent = new Intent(this, EventTriggerManager.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 12345678, intent, 0);
Here eventTriggerManager is my receiver class. But alarm is not recevied by application
Click to expand...
Click to collapse
Try this
PHP:
int time;
PendingIntent pi;
BroadcastReceiver br;
AlarmManager am;
br = new BroadcastReceiver() {
@Override
public void onReceive(Context c, Intent i) {
yourMethod();
//called when alarmmanager triggers pending intent
}
};
registerReceiver(br, new IntentFilter("com.your.packagename"));
pi = PendingIntent.getBroadcast(this, 0, new Intent(
"com.your.packagename"), 0);
am = (AlarmManager) (this.getSystemService(Context.ALARM_SERVICE));
if (you want repeating alarm ?) {
am.setRepeating(AlarmManager.RTC, System.currentTimeMillis(),
1000 * time, pi);
} else {
am.set(AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime() + 1000 * time, pi);
}
And please use the CODE tags, next time you want to post some code
gh0stslayer said:
Try this
PHP:
int time;
PendingIntent pi;
BroadcastReceiver br;
AlarmManager am;
br = new BroadcastReceiver() {
@Override
public void onReceive(Context c, Intent i) {
yourMethod();
//called when alarmmanager triggers pending intent
}
};
registerReceiver(br, new IntentFilter("com.your.packagename"));
pi = PendingIntent.getBroadcast(this, 0, new Intent(
"com.your.packagename"), 0);
am = (AlarmManager) (this.getSystemService(Context.ALARM_SERVICE));
if (you want repeating alarm ?) {
am.setRepeating(AlarmManager.RTC, System.currentTimeMillis(),
1000 * time, pi);
} else {
am.set(AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime() + 1000 * time, pi);
}
And please use the CODE tags, next time you want to post some code
Click to expand...
Click to collapse
Hello,
Alarms are getting received only when activity is running it is not otherwise.
Googled and tried below flag for intent :
Code:
i.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
Also applied below permissions for app:
Code:
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"></uses-permission>
But nothing worked though. Can you please help me finding solution to it?
Sunil K said:
Hello,
Alarms are getting received only when activity is running it is not otherwise.
Googled and tried below flag for intent :
Code:
i.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
Also applied below permissions for app:
Code:
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"></uses-permission>
But nothing worked though. Can you please help me finding solution to it?
Click to expand...
Click to collapse
If you want to keep it running in the background you should put that code in a background service. You can get lots of examples on google. Start the service when you want to use it. You can use boot receiver to start the service if you want the alarm intent to start every time the app boots.
Sent from my Galaxy Nexus using Tapatalk
gh0stslayer said:
If you want to keep it running in the background you should put that code in a background service. You can get lots of examples on google. Start the service when you want to use it. You can use boot receiver to start the service if you want the alarm intent to start every time the app boots.
Sent from my Galaxy Nexus using Tapatalk
Click to expand...
Click to collapse
Thank you, I will explore this more
Sunil K said:
Thank you, I will explore this more
Click to expand...
Click to collapse
good luck, post again if you face some problems.
Sent from my Galaxy Nexus using Tapatalk

[SOLVED] Lockscreen Widget forgets onUpdate init code after reopening the lockscreen

I made a simple widget, which has a few buttons. In order to make those buttons work, I use RemoteViews.setOnClickPendingIntent in the onUpdate method of the AppWidgetProvider. Works fine, as long as I place the widget only on the homescreen. But when using the widget on the lockscreen, I have the following problem:
Placing the widget on the lockscreen and immediately clicking the buttons works perfectly. But after closing the lockscreen and reopening it, the buttons don't work anymore. It seems like the widget had "forgotten" everything from the init stuff in the onUpdate method.
So, does somebody know how to fix this?
Some code snippets:
Code:
public class WidgetProvider extends AppWidgetProvider {
private static final String BUTTON_ALL_OFF = "buttonAllOff";
private RemoteViews remoteViews;
private ComponentName watchWidget;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
watchWidget = new ComponentName(context, WidgetProvider.class);
remoteViews.setOnClickPendingIntent(R.id.buttonAllOff, getPendingSelfIntent(context, BUTTON_ALL_OFF));
appWidgetManager.updateAppWidget(watchWidget, remoteViews);
}
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
String action = intent.getAction();
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
watchWidget = new ComponentName(context, WidgetProvider.class);
if (BUTTON_ALL_OFF.equals(action)) {
Log.d("test", "clicked");
//do fancy stuff
}
}
protected PendingIntent getPendingSelfIntent(Context context, String action) {
Intent intent = new Intent(context, getClass());
intent.setAction(action);
return PendingIntent.getBroadcast(context, 0, intent, 0);
}
}
Code:
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/widget_layout"
android:minHeight="700dp"
android:minWidth="300dp"
android:resizeMode="vertical"
android:updatePeriodMillis="0"
android:widgetCategory="home_screen|keyguard" >
</appwidget-provider>
Fixed it by adding the clicklistener also in the onReceive method

CountDownTimer problem

Hello! I'm new to this forum. I'm developing an Android app, I have some problems with the functionality of a timer and I don't figure out why. I would need some ideas.
In my application I have 2 activities: one is with the levels of a game, where you can chose between them and the second one is with the game itself. In the second activity I have a CountDownTimer which tells me when the game must finish. I have a progressBar assigned to that timer.
CountDownTimer mCountDownTimer; -global variable
In onCreate I have:
mProgressBar=(ProgressBar)findViewById(R.id.progressBar);
mProgressBar.setProgress(0);
mCountDownTimer=new CountDownTimer(90000,1000) {
int i=0;
@override
public void onTick(long millisUntilFinished) {
Log.v("Log_tag", "Tick of Progress" + i + millisUntilFinished);
i++;
mProgressBar.setProgress(i); }
@override
public void onFinish() {
i++;
mProgressBar.setProgress(i);
Intent in = new Intent(getApplicationContext(), MyActivity2.class);
startActivity(in);
}
};
mCountDownTimer.start();
I have also overriden the native back button from Android to go to the first activity, the one with the levels and there I try to stop the counter, but it doesn't seem to work at all. The counter doesn't stop, and the other functions don't work as well.
Here is the code:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) { //Back key pressed
mCountDownTimer.cancel();
Intent in = new Intent(getApplicationContext(), MyActivity2.class);
startActivity(in);
mCountDownTimer.cancel();
return true;
}
return super.onKeyDown(keyCode, event);
}
public void onBackPressed(){
mCountDownTimer.cancel();
Intent in = new Intent(getApplicationContext(), MyActivity2.class);
startActivity(in);
return;
How could I solve this? Thank you so much!
Here is your problem:
CountDownTimer mCountDownTimer; -global variable
There are no global variables in Java, only instance variables. Since you didn't post your full code I'm going to make a few assumptions here...
public class myAwesomeTimerClass {
CountDownTimer mCountDownTimer;
// bla bla
}
Then in your second activity/class, you should refer to the class:
instead of:
mCountDownTimer.cancel();
try:
myAwesomeTimerClass.mCountDownTimer.cancel();
Good luck.

Categories

Resources