如何在另一个线程中创建 [=f10=] 通知?

How do I create an Android notificaton in another thread?

我想为我的 android 应用创建通知。当我 运行 它与我的其余代码在 class 中时,我让它工作正常,当我单击一个按钮并且通知工作正常时,一切都会执行。但是,因为有很多工作要做,所以当我单击该按钮时,应用程序正在 运行ning 缓慢。为了帮助解决这个问题,我将通知部分分成了自己的 class 部分,并且 运行 将其放在不同的线程中。 我没有太多关于线程或通知的经验,我希望能为这个问题提供一些帮助。我应该将通知部分分成 class 吗?

HomeActivity class 是调用线程启动的地方,在单击按钮时使用 start() 方法:

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import Model.Data;

public class HomeActivity extends FragmentActivity {

private com.atmlocator.hooper.kenneth.atmlocator.Notification n;

public void addListenerOnButton() {

        locationSpinner = (Spinner) findViewById(R.id.spinner1);
        options1Spinner = (Spinner) findViewById(R.id.spinner2);
        options2Spinner = (Spinner) findViewById(R.id.spinner3);
        Button locate = (Button) findViewById(R.id.locateBtn);

        locate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            Context context = getApplicationContext();
            String text = "Loading...";

            //call notification thread here
            n.start();

            //rest of code here...
            }
        }
    }
}

然后通知 class 是:

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.FragmentActivity;

public class Notification extends FragmentActivity implements Runnable {

    private Thread t;
    private String name;
    Context context;

    Notification(String name, Context c)
    {
        this.name = name;
        this.context = c;
    }

    public void run()
    {
        // Intent is triggered if the notification is selected
        Intent intent = new Intent(context, HomeActivity.class);
        PendingIntent pIntent = PendingIntent.getActivity(context, (int) System.currentTimeMillis(), intent, 0);

        // Build notification
        android.app.Notification not = new android.app.Notification.Builder(context)
            .setContentTitle("Notification")
            .setContentText("New email").setSmallIcon(R.drawable.atm)
            .setContentIntent(pIntent).build();
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        not.flags |= android.app.Notification.FLAG_AUTO_CANCEL;

        notificationManager.notify(0, not);
    }

    public void start()
    {
        t = new Thread (this, name);
        t.start ();
    }
}

Should I be separating the notification part into a class of it's own?

这个应该没有问题!

另外,您应该在线程外显示您的通知。 您可以使用处理程序代替 thread

来显示通知

像这样:

   }
....
 // write this outside your code block
 Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
    super.handleMessage(msg);
    showNotification();
}
};
// call it like this instead of thread start
 mHandler.sendEmptyMessage(0);

查看此 link 了解更多信息:https://developer.android.com/training/multiple-threads/communicate-ui.html

这几乎总是有效:

public static void toast(final String text, final int length, final Context context) {
    new Handler(Looper.getMainLooper()).post(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(context, text, length).show();
        }
    });
}