如何在 Notification Click 上调用非 activity 方法
How to call a non-activity method on Notification Click
我有一个 java class MyClass
,其中包含一个名为 callMethod
的方法。我想在用户点击通知时调用这个方法
下面是我用来生成通知的代码
public class MainActivity extends AppCompatActivity {
Button button;
NotificationManager mNotifyMgr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
mNotifyMgr = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, new Intent(MainActivity.this, MyClass.class), PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification =
new NotificationCompat.Builder(MainActivity.this)
.setContentTitle("Notification")
.setSmallIcon(R.drawable.ic_launcher)
.setContentText("Downloaded")
.setContentIntent(pendingIntent)
.build();
mNotifyMgr.notify(1,notification);
}
});
}
}
下面是MyClass
的实现
public class MyClass {
public void callMethod(){
System.out.println("Notification clicked");
}
}
请帮助,我现在陷入了一段时间
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
//notification callbacks here in activity
//Call method here from non activity class.
Classname.methodName();
}
你可以这样做:
创建 PendingIntent
以放入 Notification
时:
Intent notificationIntent = new Intent(MainActivity.this, MyClass.class);
notificationIntent.putExtra("fromNotification", true);
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
现在,在MyClass.onCreate()
:
if (getIntent().hasExtra("fromNotification")) {
callMethod();
}
我有一个 java class MyClass
,其中包含一个名为 callMethod
的方法。我想在用户点击通知时调用这个方法
下面是我用来生成通知的代码
public class MainActivity extends AppCompatActivity {
Button button;
NotificationManager mNotifyMgr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
mNotifyMgr = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, new Intent(MainActivity.this, MyClass.class), PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification =
new NotificationCompat.Builder(MainActivity.this)
.setContentTitle("Notification")
.setSmallIcon(R.drawable.ic_launcher)
.setContentText("Downloaded")
.setContentIntent(pendingIntent)
.build();
mNotifyMgr.notify(1,notification);
}
});
}
}
下面是MyClass
public class MyClass {
public void callMethod(){
System.out.println("Notification clicked");
}
}
请帮助,我现在陷入了一段时间
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
//notification callbacks here in activity
//Call method here from non activity class.
Classname.methodName();
}
你可以这样做:
创建 PendingIntent
以放入 Notification
时:
Intent notificationIntent = new Intent(MainActivity.this, MyClass.class);
notificationIntent.putExtra("fromNotification", true);
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
现在,在MyClass.onCreate()
:
if (getIntent().hasExtra("fromNotification")) {
callMethod();
}