Following this tutorial I quickly created a simple widget which displays the current time on the home screen:
I've modified the code so that the widget also comes with a button. My goal is to launch an activity, once the button get's pressed.
Unfortunately, I do not really understand where to listen to the button click. Does this have to go into the broadcast receiver section of the manifest file?
<!-- Broadcast Receiver -->
<receiver android:name=".WifiSSIDWidget" android:label="#string/app_name">
<intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
</intent-filter>
<meta-data android:name="android.appwidget.provider" android:resource="#xml/wifi_ssid_widget_provider" />
</receiver>
Or does the code rather go into onUpdate? Here's the widget class code:
public class HelloWidget extends AppWidgetProvider {
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
Timer timer = new Timer();
timer.scheduleAtFixedRate(new MyTime(context, appWidgetManager), 1, 1000);
}
private class MyTime extends TimerTask {
RemoteViews remoteViews;
AppWidgetManager appWidgetManager;
ComponentName thisWidget;
DateFormat format = SimpleDateFormat.getTimeInstance(SimpleDateFormat.MEDIUM, Locale.getDefault());
public MyTime(Context context, AppWidgetManager appWidgetManager) {
this.appWidgetManager = appWidgetManager;
remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
thisWidget = new ComponentName(context, HelloWidget.class);
}
#Override
public void run() {
remoteViews.setTextViewText(R.id.widget_textview, "TIME = " +format.format(new Date()));
appWidgetManager.updateAppWidget(thisWidget, remoteViews);
}
}
}
Here's the code that I want to be called after the button is clicked:
public void openWifiSettings() {
final Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
final ComponentName cn = new ComponentName("com.android.settings", "com.android.settings.wifi.WifiSettings");
intent.setComponent(cn);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
import android.app.PendingIntent;
...
public static final String ACTION_BUTTON1_CLICKED = "com.example.myapp.BUTTON1_CLICKED";
in onUpdate add the following to your button:
Intent intent = new Intent(ACTION_BUTTON1_CLICKED);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
remoteViews.setOnClickPendingIntent(R.id.button_1, pendingIntent);
and in onReceive of your AppWidget
#Override
public void onReceive(Context context, Intent intent)
{
Log.d(TAG, "onReceive() " + intent.getAction());
super.onReceive(context, intent);
...
if (ACTION_BUTTON1_CLICKED.equals(intent.getAction()))
{
// your code
}
also add your intent to the manifest
<intent-filter>
<action android:name="com.example.myapp.BUTTON1_CLICKED" />
....
Related
So I'm trying to set up a widget with a list but clicks aren't working, so I understand to do this I need a template pending intent as individual items cannot have their own, and then add to the pending intent template using a fill in intent, I've added all this to my app but it still isn't working, I've followed advice here and here but when I click on an item its just highlighted to show it has registered a click and does nothing but I do get this in the
logcat
08-06 17:02:08.001 2163-3666/? W/Binder: Binder call failed.
java.lang.NullPointerException: Attempt to read from field
'com.android.server.appwidget.AppWidgetServiceImpl$ProviderId
com.android.server.appwidget.AppWidgetServiceImpl$Provider.id'
on a null object reference
at
com.android.server.appwidget.AppWidgetServiceImpl$AppWidgetManagerLocal
.getHostedWidgetPackages(AppWidgetServiceImpl.java:4800)
at
com.android.server.accessibility.AccessibilityManagerService$SecurityPolicy
.resolveValidReportedPackageLocked(AccessibilityManagerService.java:4637)
at com.android.server.accessibility.AccessibilityManagerService
.sendAccessibilityEvent(AccessibilityManagerService.java:531)
at android.view.accessibility.IAccessibilityManager$Stub
.onTransact(IAccessibilityManager.java:71)
at android.os.Binder.execTransact(Binder.java:690)
so here is my code, my onUpdate method calls a static update widget method that defines my template pending intent, and my onReceive method
AppWidgetProvider
public class MessageMeAppWidget extends AppWidgetProvider {
private static final String TAG = "MsgMeWdgt";
#Override
public void onReceive(Context context, Intent intent) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
if (intent.getAction().equals(Constants.LAUNCH_ACTIVITY)) {
int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
//String recipientId = intent.getStringExtra(Constants.FS_ID);
//String recipientName = intent.getStringExtra(Constants.FS_NAME);
Intent i = new Intent(context, MessageListActivity.class);
Bundle b = new Bundle();
//b.putString(Constants.FS_ID, recipientId);
//b.putString(Constants.FS_ID, recipientName);
i.putExtras(b);
Bundle bundle = intent.getExtras();
String toastText = "position is " + bundle.getInt(Constants.LAUNCH_ACTIVITY);
Toast.makeText(context,toastText,Toast.LENGTH_SHORT).show();
//context.startActivity(i);
}else if(intent.getAction().equals(Constants.DONT_LAUNCH_ACTIVITY)){
//Toast.makeText(context,"",Toast.LENGTH_SHORT).show();
}
super.onReceive(context, intent);
}
public static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
SharedPreferences sharedPreferences = context.getSharedPreferences(Constants.SHARED_PREFS,Context.MODE_PRIVATE);
boolean userSignedIn = sharedPreferences.getBoolean(Constants.SIGNED_IN, false);
if (userSignedIn){
//move everything in here after i know its working
}
Intent intent = new Intent(context, MessagingService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_list_view);
//populates data
remoteViews.setRemoteAdapter(R.id.widget_list,intent);
//remoteViews.setRemoteAdapter(appWidgetId, R.id.widget_list, intent);
remoteViews.setEmptyView(R.id.widget_list,R.id.empty);
Intent launchIntent = new Intent(context, WidgetAdapter.class);
launchIntent.setAction(Constants.LAUNCH_ACTIVITY);
launchIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
PendingIntent launchPendingIntent = PendingIntent.getBroadcast(context, 0, launchIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setPendingIntentTemplate(R.id.widget_list, launchPendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// There may be multiple widgets active, so update all of them
Log.d(TAG,"on update");
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}
Then my widget service is standard
public class MessagingService extends RemoteViewsService {
private String TAG = "MsgSrvc";
#Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
Log.d(TAG,"item clicked");
return new WidgetAdapter(this.getApplicationContext(), intent);
}
}
and then in the getViewAt method of my widget adapter I fill my fill in intent like this
Bundle extras = new Bundle();
extras.putInt(Constants.LAUNCH_ACTIVITY, position);
Intent fillInIntent = new Intent();
fillInIntent.putExtras(extras);
remoteViews.setOnClickFillInIntent(R.id.holder, fillInIntent);
in my manifest I'm registering my widget service and the receiver
<receiver android:name="com.sealstudios.aimessage.MessageMeAppWidget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="#xml/message_me_app_widget_info" />
</receiver>
<service android:name=".MessagingService"
android:permission="android.permission.BIND_REMOTEVIEWS" />
any help appreciated
i fixed this it wasthe intent was pointing to the wrong class so
Intent launchIntent = new Intent(context, WidgetAdapter.class);
should have been
Intent launchIntent = new Intent(context, MessageMeAppWidget.class);
I want 2 separate layouts for homescreen and lockscreen.
I have read https://developer.android.com/guide/topics/appwidgets/index.html#lockscreen
But it is unclear where to implement this and how to change the layout at runtime for both homescreen and lockscreen?
I would be grateful if there is clear tutorial / example to do this.
Thanks
If you know home screen widget implementation, it's easy.
From the code below you can figure out how to use different layout for lock screen and home screen to display the current time every second.
Create different layout for different widgets
#layout/widget_keyguard //For lock screen widget
#layout/widget_home //For home screen widget
Note: Use one TextView with id time_view on both the layouts to display the time
xml/widget_info.xml
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialKeyguardLayout="#layout/widget_keyguard" // layout for lock screen
android:initialLayout="#layout/widget_home" // layout for lock screen (if not provided) & home screen
android:minHeight="100dp"
android:minWidth="300dp"
android:previewImage="#drawable/ic_launcher"
android:resizeMode="none"
android:updatePeriodMillis="180000"
android:widgetCategory="keyguard|home_screen" > //Enable widgets on both home screen and lock screen
</appwidget-provider>
AppWidgetProvider.java
public class TestAppWidgetProvider extends AppWidgetProvider {
#Override
public void onDeleted(Context context, int[] appWidgetIds) {
super.onDeleted(context, appWidgetIds);
}
#Override
public void onDisabled(Context context) {
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
PendingIntent sender = PendingIntent
.getBroadcast(context, 0, intent, 0);
AlarmManager am = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
am.cancel(sender); //When all the widgets are disabled, do not forget to cancel the service
super.onDisabled(context);
}
#Override
public void onEnabled(Context context) {
super.onEnabled(context);
Toast.makeText(context, "Widget Enabled", Toast.LENGTH_SHORT).show();
//AlarmManager to update the widgets
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
PendingIntent p_intent = PendingIntent.getBroadcast(context, 0, intent,
0);
AlarmManager am = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
// Here I am updating the widgets every second (1000 ms) , you can use however you want
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
1000, p_intent);
}
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
Toast.makeText(context, "Widget Updated", Toast.LENGTH_SHORT).show();
ComponentName thisWidget = new ComponentName(context,
TestAppWidgetProvider.class);
for (int widgetId : appWidgetManager.getAppWidgetIds(thisWidget)) {
Bundle myOptions = appWidgetManager.getAppWidgetOptions(widgetId);
int category = myOptions.getInt(
AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY, -1);
RemoteViews remoteViews;
if (category == AppWidgetProviderInfo.WIDGET_CATEGORY_KEYGUARD) {
// Get the remote views
remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget_keyguard);
}
else {
remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget_home);
}
SimpleDateFormat dateFormat = new SimpleDateFormat(
"HH:mm:ss", Locale.US);
// use TextView with time_view id on both home screen & lock screen layouts
remoteViews.setTextViewText(R.id.time_view,
dateFormat.format(new Date(System.currentTimeMillis())));
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
}
#Override
public void onAppWidgetOptionsChanged(Context context,
AppWidgetManager appWidgetManager, int appWidgetId,
Bundle newOptions) {
}
}
AlarmManagerBroadcastReceiver class
public class AlarmManagerBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
ComponentName thiswidget = new ComponentName(context,
TestAppWidgetProvider.class);
AppWidgetManager appWidgetManager = AppWidgetManager
.getInstance(context);
for (int widgetId : appWidgetManager.getAppWidgetIds(thiswidget)) {
Bundle myOptions = appWidgetManager.getAppWidgetOptions(widgetId);
int category = myOptions.getInt(
AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY, -1);
RemoteViews remoteViews;
if (category == AppWidgetProviderInfo.WIDGET_CATEGORY_KEYGUARD) {
// Get the remote views
Log.d("Widget", "Lockscreen widget");
remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget_keyguard);
}
else {
Log.d("Widget", "Homescreen widget");
remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget_home);
}
SimpleDateFormat dateFormat = new SimpleDateFormat(
"HH:mm:ss", Locale.US);
// use TextView with time_view id on both home screen & lock screen layouts
remoteViews.setTextViewText(R.id.time_view,
dateFormat.format(new Date(System.currentTimeMillis())));
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
}
}
Finally, do not forget to update the manifest file
<application
...
<receiver android:name=".AlarmManagerBroadcastReceiver" />
<receiver android:name=".TestAppWidgetProvider" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="#xml/widget_info" />
</receiver>
.....
</application>
that's it.
I hope it'll help you to solve your problem
I create a keyguard widget that has a button that upsate textview with Random number when clicking. it works properly on the home screen but on the lock screen (android 4.2.2) it(s button) works just when i add it to my lock screen widgets but when i turn screen off and return; it(s button) doesn't work !!
Widget.java
public class Widget extends AppWidgetProvider {
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
Timer timer = new Timer();
timer.scheduleAtFixedRate(new MyTime(context, appWidgetManager), 1, 50000);
}
class MyTime extends TimerTask {
RemoteViews remoteViews;
AppWidgetManager appWidgetManager;
ComponentName thisWidget;
Context context;
public MyTime(Context context, AppWidgetManager appWidgetManager) {
this.appWidgetManager = appWidgetManager;
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
thisWidget = new ComponentName(context, Widget.class);
this.context=context;
}
#Override
public void run() {
Intent configIntent = new Intent(context, WidgetReceive.class);
configIntent.setAction("updateTextView");
PendingIntent configPendingIntent = PendingIntent.getBroadcast(context, 0, configIntent, 0);
remoteViews.setOnClickPendingIntent(R.id.button1, configPendingIntent);
remoteViews.setTextViewText(R.id.widget_textview, String.valueOf(new Random().nextInt(100)));
appWidgetManager.updateAppWidget(thisWidget, remoteViews);
}
}
}
WidgetReceive.java
public class WidgetReceive extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
ComponentName thisWidget = new ComponentName(context, Widget.class);
if(intent.getAction().equals("updateTextView")){
remoteViews.setTextViewText(R.id.widget_textview, String.valueOf(new Random().nextInt(100)));
appWidgetManager.updateAppWidget(thisWidget, remoteViews);
}
}
}
AndroidManifest.xml
<receiver android:name=".Widget" android:label="#string/app_name">
<intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
</intent-filter>
<meta-data android:name="android.appwidget.provider" android:resource="#xml/hello_widget_provider" />
</receiver>
<receiver android:name=".WidgetReceive">
<intent-filter>
<action android:name="updateTextView"/>
</intent-filter>
</receiver>
The problem might be that the following line
remoteViews.setOnClickPendingIntent(R.id.button1, configPendingIntent);
is missing from WidgetReceive.onReceive(...)
When updating an app widget with a RemoteViews object, you must create all the contents of the widget, including the listeners, every time.
I want to make a very simple widget:
It must consist from just one image view
1) on incoming sms it should change the image
2) on click it also should change the image
I tried to make it using ImageButton but failed: there were problems with changing the image on sms received event: new image had wrong scale.
Anyway now I want to make an ImageView without anything else.
The problem is that I can't handle onClick event:
I've got a running service which should handle all events: sms received and click:
widget provider:
public class MyWidgetProvider extends AppWidgetProvider {
#Override
public void onDisabled(Context context) {
context.stopService(new Intent(context, UpdateService.class));
super.onDisabled(context);
}
#Override
public void onEnabled(Context context) {
super.onEnabled(context);
Intent intent = new Intent(context, UpdateService.class);
context.startService(intent);
}
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
for (int i = 0; i < appWidgetIds.length; i++) {
int appWidgetId = appWidgetIds[i];
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
Intent widgetClickIntent = new Intent(context, UpdateService.class);
widgetClickIntent.setAction(UpdateService.ACTION_ON_CLICK);
PendingIntent pendingIntentViewClick = PendingIntent.getService(context, 0, widgetClickIntent, 0);
remoteViews.setOnClickPendingIntent(R.id.widget_imageview, pendingIntentViewClick);
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}
}
}
service:
public class UpdateService extends Service {
static final String ACTION_SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
static final String ACTION_ON_CLICK = "android.MyWidget.ACTION_ON_CLICK";
private final static IntentFilter intentFilter;
static {
intentFilter = new IntentFilter();
intentFilter.addAction(ACTION_SMS_RECEIVED);
intentFilter.addAction(ACTION_ON_CLICK);
}
public final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (action.equals(ACTION_SMS_RECEIVED)) {
reactOnSms(context);
}
if (action.equals(ACTION_ON_CLICK)) {
onCLick(context);
}
}
};
#Override
public void onCreate() {
super.onCreate();
registerReceiver(broadcastReceiver, intentFilter);
}
#Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(broadcastReceiver);
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
private void reactOnSms(Context context) {
// doSomething
}
public void onCLick(Context context) {
// doSomething
}
Manifest:
<receiver a:name="..."...>
<intent-filter>
<action a:name="android.provider.Telephony.SMS_RECEIVED"/>
<action a:name="android.MyWidget.ACTION_ON_CLICK"/>
</intent-filter>
<meta-data a:name="android.appwidget.provider"
a:resource="#xml/my_widget_provider_info"/>
</receiver>
<service a:name=".UpdateService"
a:label="UpdateService">
<intent-filter>
<action a:name="android.provider.Telephony.SMS_RECEIVED"/>
<action a:name="android.MyWidget.ACTION_ON_CLICK"/>
</intent-filter>
</service>
I tried this Clickable widgets in android
I found the solution.
Sorry for those of you who read the question. Too much code inside, I understand.
The problem was that UpdateService was not the real handler of the broadcast intent. Anonymous implementation of BroadcastReceiver made all the work.
So the problem was in this code (widgetProvider):
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
for (int i = 0; i < appWidgetIds.length; i++) {
int appWidgetId = appWidgetIds[i];
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
// wrong:
// Intent widgetClickIntent = new Intent(context, UpdateService.class);
// widgetClickIntent.setAction(UpdateService.ACTION_ON_CLICK);
// PendingIntent pendingIntentViewClick = PendingIntent.getService(context, 0, widgetClickIntent, 0);
// correct:
Intent widgetClickIntent = new Intent(UpdateService.ACTION_ON_CLICK);
PendingIntent pendingIntentViewClick = PendingIntent.getBroadcast(context, 0, widgetClickIntent, 0);
///////
remoteViews.setOnClickPendingIntent(R.id.widget_imageview, pendingIntentViewClick);
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}
}
Hi i've never worked with widgets before but what i'm looking to do is create a very simple widget i basically want to make a 1 by 1 widget that has just an icon, just an image set as the background no text nothing just a small icon and when the icon is pressed i want to open an activity. Basically i want to make a second icon like in the app drawer in a widget form that opens another activity rather than the main one.
Any help is greatly appreciated
My provider ended up looking like this after a lot of research and playing
public class WidgetProvider extends AppWidgetProvider {
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int N = appWidgetIds.length;
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];
Intent intent = new Intent(context, ClassToLaunchHere.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
views.setOnClickPendingIntent(R.id.widget, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
}
Couple of app widget implementations that show the easiest way to do that are visible at https://github.com/commonsguy/cw-advandroid/tree/master/AppWidget.
Specifically, https://github.com/commonsguy/cw-advandroid/blob/master/AppWidget/PairOfDice/src/com/commonsware/android/appwidget/dice/AppWidget.java shows how to use a PendingIntent as the onClick target for a Button. You can make your PendingIntent start an Activity and you should be good to go.
Declare a Variable
public static String YOUR_AWESOME_ACTION = "YourAwesomeAction";
then add onUpdate and onReceive
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
ComponentName thisWidget = new ComponentName(context, DigitalClock.class);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.digital_clock);
for (int widgetId : appWidgetManager.getAppWidgetIds(thisWidget)) {
remoteViews.setOnClickPendingIntent(R.id.imageView, getPendingSelfIntent(context, YOUR_AWESOME_ACTION));
appWidgetManager.updateAppWidget(thisWidget, remoteViews);
}
}
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
super.onReceive(context, intent);
if (YOUR_AWESOME_ACTION.equals(intent.getAction())) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.digital_clock);
ComponentName watchWidget = new ComponentName(context, DigitalClock.class);
appWidgetManager.updateAppWidget(watchWidget, remoteViews);
Intent ntent = new Intent(context, MainActivity.class);
ntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(ntent);
//Toast.makeText(context, YOUR_AWESOME_ACTION, Toast.LENGTH_SHORT).show();
}
}
protected PendingIntent getPendingSelfIntent(Context context, String action) {
Intent intent = new Intent(context, getClass());
intent.setAction(action);
return PendingIntent.getBroadcast(context, 0, intent, 0);
}
and add activity on Manifest
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
hope this will help