extras(notification text) doesn't get passed - java

the extras ( a notification text) doesn't get passed to the textView, (the text view appears empty), what's the problem?
here's the related code:
AlarmReceiver extends BroadcastReceiver :
https://textuploader.com/1j851
Notification Activity( that should show the text):
https://textuploader.com/1j85j
my main activity:
https://textuploader.com/1j8tr
please excuse me if I did any stupid mistakes, I'm new to programming

I guess this is your problem here. You pass the wrong value here. You make an object i but you are using intent object.
Intent i = new Intent(context,
NotificationActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.putExtra("message",x);
PendingIntent pendingIntent = PendingIntent.getActivity(context,0,i,PendingIntent.FLAG_UPDATE_CURRENT);
So use like this below.
Intent i = new Intent(context,
NotificationActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK); //i
i.putExtra("message",x); //i
PendingIntent pendingIntent = PendingIntent.getActivity(context,0,i,PendingIntent.FLAG_UPDATE_CURRENT);

Related

Android - Run a system settings activity

If I run this code
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(new ComponentName("com.android.settings",
"com.android.settings.Settings$NotificationAppListActivity"));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
I get this
PICTURE 1
Instead,I want that the startActivity(intent) opens the activity that appears when I tap on the "Phone" view in the list
PICTURE 2
How can I get this ? Thanks
Use This..
Intent intent = new Intent(android.provider.Settings.ACTION_SETTINGS);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Or Edit your code line 2
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(new ComponentName("com.android.settings",
"com.android.settings.Settings"));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Because ComponentName method get 2 parameter 1. package name and 2. class name ComponentName(String pkg, String cls) first one, package name is ok but class name com.android.settings.Settings$NotificationAppListActivity that means you want to open Notification settings
try this way
Intent intent = new Intent();
intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
intent.putExtra("app_package", getPackageName());
intent.putExtra("app_uid", getApplicationInfo().uid);
startActivity(intent);
Use this:
startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));

Notification PendingIntent hard bug (or really simple one)

I'm trying to debug this for ages now and I just can't seem to find the problem:
I have a broadcast receiver, which receives the broadcast successfully.
The notification has two actions ("buttons"):
firstIntent = null;
secondIntent = null;
firstPendingIntent = null; //first "button" to join with the first intent
secondPendingIntent = null; //second "button" to join with the second intent
if(boolean){
//relevant
firstIntent = new Intent(getBaseContext(), NotificationFunctions.class).putExtra("action", "do_this");
secondIntent = new Intent(getBaseContext(), NotificationFunctions.class).putExtra("action", "do_that");
}else{
firstIntent = new Intent(getBaseContext(), NotificationFunctions.class).putExtra("action", "do_another_this");
secondIntent = new Intent(getBaseContext(), NotificationFunctions.class).putExtra("action", "do_another_that");
}
firstPendingIntent = PendingIntent.getBroadcast(getBaseContext(), 0, firstIntent, PendingIntent.FLAG_UPDATE_CURRENT);
secondPendingIntent = PendingIntent.getBroadcast(getBaseContext(), 0, secondIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification = new NotificationCompat.Builder(getApplicationContext())
.setContentTitle(notification_title)
.setContentText(notification_text)
.setTicker("Notification!")
.setWhen(System.currentTimeMillis())
.setDefaults(Notification.DEFAULT_SOUND)
.addAction(R.drawable.abc_cab_background_top_holo_light, first_option, firstPendingIntent)
.addAction(R.drawable.abc_cab_background_top_holo_dark, second_option, secondPendingIntent)
.setAutoCancel(true)
.setSmallIcon(R.drawable.ic_stat_name)
.build();
Whenever I debug in the broadcastReceiver, for some reason, action from extras always logs "do_that", even if I click the first or second button of the notification. Any reason for this? I cant really seem to understand why.
public class NotificationFunctions extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
boolean feedback;
String action = intent.getExtras().getString("action");
Log.wtf("...", action); //logs do_that
}}
Any reason for this?
Because firstPendingIntent == secondPendingIntent.
If there already is a PendingIntent matching your request, getBroadcast() returns the existing PendingIntent. FLAG_UPDATE_CURRENT says to replace the extras in the Intent wrapped inside the PendingIntent.
In one of your two getBroadcast() calls, replace 0 with some other number, to get distinct PendingIntent objects.
Also, I recommend that you replace getBaseContext() and getApplicationContext() with this. Only use getBaseContext() and getApplicationContext() when you know precisely why you are using them.

Restart MainActivity or application with singleTop.

How can I restart my application or MainActivity when the activity have:
android:launchMode="singleTop"
I have tried:
Intent i = getBaseContext().getPackageManager().getLaunchIntentForPackage( getBaseContext().getPackageName() );
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
But because of singleTop this not working, there is an other way to do this?
I did my theme switcher like this:
Intent intent = getIntent();
finish();
startActivity(intent);
Basically, I'm calling finish() first, and I'm using the exact same intent this activity was started with. That seems to do the trick?
UPDATE: As pointed out by Ralf below, Activity.recreate() is the way to go in API 11 and beyond. This is preferable if you're in an API11+ environment. You can still check the current version and call the code snippet above if you're in API 10 or below. (Please don't forget to up-vote Ralf's answer!)
public void reload() {
Intent intent = getIntent();
overridePendingTransition(0, 0);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();
overridePendingTransition(0, 0);
startActivity(intent);
}
or
private void restartFirstActivity()
{
Intent i = getApplicationContext().getPackageManager()
.getLaunchIntentForPackage(getApplicationContext().getPackageName() );
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK );
startActivity(i);
}
try to restart with this may this will help u
Intent i = cntxt.getPackageManager().getLaunchIntentForPackage(cntxt.getPackageName() );
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addCategory(Intent.CATEGORY_HOME);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
cntxt.startActivity(i);
Use this:
Intent finishIntent = new Intent( this,
activity_that_you_want_to_return.class);
finishIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(finishIntent);

Recreate Activity when pressing the notification - how to?

I have a clickable notification, that opens my application when clicked. Howewer, old activity isn't deleted, so, when I close app, there's the same activity under previous...hope you understand.
So, how can I delete old activity?
The code below is situated in service. And I need to destroy Main activity.
void sendNotif() {
Intent in = new Intent(this, MainActivity.class);
PendingIntent pin = PendingIntent.getActivity(this, 0, in, 0);
Notification notif = new Notification.Builder(this)
.setContentTitle("App launched")
.setContentText("Press to open app")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pin)
.build();
notif.flags |= Notification.FLAG_NO_CLEAR;
nm.notify(1, notif);
}
Kotlin:
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
val pendingIntent = PendingIntent.getActivity(applicationContext, 0,
intent, PendingIntent.FLAG_CANCEL_CURRENT)
Why don't you just add a flag to your Intent when you create your PendingIntent like this:
Intent in = new Intent(this, getClass()).addFlags(Intent.FLAG_CANCEL_CURRENT));
PendingIntent pin = PendingIntent.getActivity(this, 0, in, 0);
Try adding these flags to your intent.
in.setFlags(FLAG_ACTIVITY_CLEAR_TASK | FLAG_ACTIVITY_NEW_TASK);
See this for explanation.
onPause()
{
MainActivity.this.finish();
}

Get appwidget position on home screen?

I start an activity when a widget icon is clicked.What i want is to send the position of that widget to that activity.I have tried
Intent intent=new Intent(context, WidgetActivity.class);
Bundle bundle = new Bundle();
bundle.putString("rect", bound);
intent.putExtras(bundle);
//intent.putExtra("rect", bound.toString());
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,intent, 0);
RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.pic1);
views.setOnClickPendingIntent(R.id.wbutton1, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
in my onUpdate procedure.But this crashes the activity.
I have heard that the position is returned using intent.getsourceBounds() .Thats in onReceive ryt?i have tried making bound a global variable.setting its value from onReceive as bound=intent.getsourceBounds(); That didn't work either.
So my question is
1.How do i get the widget position using intent.getsourceBounds() in onUpdate?
2.How can i send it to the activity?
Thank you for your time.
edit:i managed to send string from widget to activity like this:
Intent intent=new Intent(context, WidgetActivity.class);
Bundle bundle = new Bundle();
bundle.putString("pos", "hello");
intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(context,0,intent,PendingIntent.FLAG_CANCEL_CURRENT);
edit 2: I found https://stackoverflow.com/a/5324918/1685829 which says it can be done using getSourceBounds().Can anyone explain how i can use it in onUpdate.
You don't need to send it; Android does it for you. getSourceBounds() can only be used from inside the activity. This information is not available inside onUpdate().

Categories