【问题标题】:How to call a non-activity method on Notification Click如何在通知点击上调用非活动方法
【发布时间】:2017-02-21 03:40:45
【问题描述】:

我有一个 java 类 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");
    }
}

请帮忙,我现在陷入了一段时间

【问题讨论】:

  • 试试 MyClass.callMethod() 它会起作用,你可以有一个 Util 类来处理一些像这样的通用情况
  • @BabajideApata 我在哪里调用 MyClass.callMethod()?
  • 看这一行 PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, new Intent(MainActivity.this, MyClass.class), PendingIntent.FLAG_UPDATE_CURRENT);它的作用是调用我的类,所以在这个类中,你可以调用你的方法,类立即开始

标签: android android-intent notifications android-pendingintent


【解决方案1】:

你可以这样做:

在创建 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();
}

【讨论】:

  • MyClass不是activity类,MyClass中没有onCreate()方法
  • 那么,你不能这样做:PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, new Intent(MainActivity.this, MyClass.class), PendingIntent.FLAG_UPDATE_CURRENT); 你在你的代码中发布了!
  • 点击Notification 会触发Intent。而已。您需要一些东西来接收Intent 并执行您想要完成的操作。只有 Android 组件(ActivityServiceBroadcastReceiver)可以接收Intent。所以看起来你需要写一个ActivityServiceBroadcastReceiver 来接收Intent,然后调用MyClass.callMethod()
【解决方案2】:
 @Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    //notification callbacks here in activity
    //Call method here from non activity class.
    Classname.methodName();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-16
    相关资源
    最近更新 更多