当我的应用关闭时,如何在我的应用中阅读所有应用通知?

时间:2015-09-05 11:21:59

标签: android-activity service notifications broadcastreceiver

    package com.example.notificationlistener;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

public class NotificationService extends NotificationListenerService {

    Context context;

    @Override
    public void onCreate() {

        super.onCreate();
        context = getApplicationContext();

    }

    @Override
    public void onNotificationPosted(StatusBarNotification sbn)
    {
        Log.i("From service","onNotificationPosted");

        String pack = sbn.getPackageName();

//      if("com.twitter.android".equalsIgnoreCase(pack))
//      if("com.google.android.gm".equalsIgnoreCase(pack))
        {
            String ticker = sbn.getNotification().tickerText.toString();

            Bundle extras = sbn.getNotification().extras;

            String title = extras.getString("android.title");
            String text = extras.getCharSequence("android.text").toString();

            Log.i("Package",pack);
            Log.i("Ticker",ticker);
            Log.i("Title",title);
            Log.i("Text",text);

            Intent msgrcv = new Intent("Msg");
            msgrcv.putExtra("package", pack);
            msgrcv.putExtra("ticker", ticker);
            msgrcv.putExtra("title", title);
            msgrcv.putExtra("text", text);

            LocalBroadcastManager.getInstance(context).sendBroadcast(msgrcv);
        }

    }

    @Override
    public void onNotificationRemoved(StatusBarNotification sbn)
    {
        Log.i("Msg","Notification Removed");
    }

}

this is my service class 

    package com.example.notificationlistener;

import java.util.Locale;

import android.app.Notification;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.ActionBarActivity;
import android.text.Html;
import android.util.Log;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity implements TextToSpeech.OnInitListener{

     TableLayout tableLayout;
     TextToSpeech textToSpeech;
     String text = "noText";
     String title = "noTitle";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tableLayout = (TableLayout)findViewById(R.id.tab);
        textToSpeech = new TextToSpeech(this, null);

        LocalBroadcastManager.getInstance(this).registerReceiver(onNotice, new IntentFilter("Msg"));

    }

    private BroadcastReceiver onNotice= new BroadcastReceiver() {

        @SuppressWarnings("deprecation")
        @Override
        public void onReceive(Context context, Intent intent) {

            Log.w("From MainActivity", "broadcast receiver is called");

            String pack = intent.getStringExtra("package");
            String ticker = intent.getStringExtra("ticker");
            title = intent.getStringExtra("title");
            text = intent.getStringExtra("text");

            Log.d("mainActivity_pack", pack);
            Log.d("mainActivity_title", title);
            Log.d("mainActivity_text", text);
            Log.d("mainActivity_ticker", ticker);
//            Log.d("mainActivity", sender);

            NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

            Notification notification = new Notification(R.drawable.ic_launcher, "New Message", System.currentTimeMillis()+5000);
            notification.setLatestEventInfo(context, title, text, null);
            notification.flags |= Notification.FLAG_AUTO_CANCEL;
            notification.defaults |= Notification.DEFAULT_SOUND;

            notificationManager.notify(2, notification);

            speak();

            TableRow tr = new TableRow(getApplicationContext());
            tr.setLayoutParams(new TableRow.LayoutParams( TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
            TextView textview = new TextView(getApplicationContext());
            textview.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT,1.0f));
            textview.setTextSize(20);
            textview.setTextColor(Color.parseColor("#0B0719"));
            textview.setText(Html.fromHtml(pack +"<br><b>" + title + " : </b>" + text));
            tr.addView(textview);
            tableLayout.addView(tr);

        }
    };

    protected void onDestroy()
    {
        super.onDestroy();
        if (textToSpeech != null)
        {
            textToSpeech.stop();
            textToSpeech.shutdown();
        }

        LocalBroadcastManager.getInstance(this).unregisterReceiver(onNotice);
    }

    @Override
    public void onInit(int status) {

        if (status == TextToSpeech.SUCCESS) {

            int result = textToSpeech.setLanguage(Locale.US);

            if (result == TextToSpeech.LANG_MISSING_DATA
                    || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Log.e("TTS", "This Language is not supported");
            } else
            {

                speak();
            }

        } else {
            Log.e("TTS", "Initilization Failed!");
        }

    }

     private void speak()
     {
          textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null);
      }`enter code here`

}
这是我的活动。我正在尝试阅读通知并通过我的应用程序重新发送它们这样做了,我能够收到通知但是当我的应用程序关闭时它不起作用所以请建议我一些更改或建议...谢谢

1 个答案:

答案 0 :(得分:0)

将您的广播接收器放入服务中。应用程序关闭时,活动不会运行。服务呢。从您的活动开始您的服务。然后他们将在关闭应用程序后继续运行。