Reputation: 5069
I have a activity named as MainActivity.java which contains the Custom Notification Builder. Here is the code :
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bcustomnotify = (Button) findViewById(R.id.customnotification);
bcustomnotify.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
CustomNotification();
}
});
}
public void CustomNotification() {
// Using RemoteViews to bind custom layouts into Notification
RemoteViews remoteViews = new RemoteViews(getPackageName(),
R.layout.customnotification);
// Set Notification Title
String strtitle = getString(R.string.customnotificationtitle);
// Set Notification Text
String strtext = getString(R.string.customnotificationtext);
// Open NotificationView Class on Notification Click
Intent intent = new Intent(this, NotificationView.class);
// Send data to NotificationView Class
intent.putExtra("title", strtitle);
intent.putExtra("text", strtext);
// Open NotificationView.java Activity
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
// Set Icon
.setSmallIcon(R.mipmap.ic_launcher)
// Set Ticker Message
.setTicker(getString(R.string.customnotificationticker))
// Dismiss Notification
.setOngoing(true)
// Set PendingIntent into Notification
.setContentIntent(pIntent)
// Set RemoteViews into Notification
.setContent(remoteViews);
// Locate and set the Image into customnotificationtext.xml ImageViews
remoteViews.setImageViewResource(R.id.imagenotileft, R.mipmap.ic_launcher);
remoteViews.setImageViewResource(R.id.imagenotiright, R.mipmap.ic_launcher);
// Locate and set the Text into customnotificationtext.xml TextViews
remoteViews.setTextViewText(R.id.title, getString(R.string.customnotificationtitle));
remoteViews.setTextViewText(R.id.text, getString(R.string.customnotificationtext));
// Create Notification Manager
NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Build Notification with Notification Manager
notificationmanager.notify(0, builder.build());
}
}
and I have a class in another activity named as anotherActivity.java which goes like this :
public class anotherActivity extends Activity {
// some code
}
Now the Question is
Is it possible to run the class of another activity after the notification of MainActivity is clicked without actually opening the another activity as pending intent?
Upvotes: 1
Views: 64
Reputation: 1006644
Is it possible to run the class of another activity after the notification of MainActivity is clicked without actually opening the another activity as pending intent?
No.
If you want tapping on a Notification
to do something with no visible UI, do not use getActivity()
on PendingIntent
. Use getService()
or getBroadcast()
and route to an appropriate component.
If you are saying that you want a tap on the Notification
to show one activity (apparently named NotificationView
) and also do something from AnotherActivity
, then AnotherActivity
should be merged into NotificationView
, or the common code refactored into another class, or something. Your PendingIntent
can do one thing: start an activity, start a service, or send a broadcast.
Upvotes: 2