0
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` 

} 

这是我的活动。我想读的通知,并通过我的应用程序重新发送它们,以便做到了这一点,我能够得到通知,但是当我的应用程序被关闭其没有工作,所以请建议我一些改变或建议...谢谢如何在应用程序关闭时阅读我应用程序中的所有应用程序通知?

回答

0

把你的将接收机广播成服务。应用程序关闭时,活动不会运行。服务确实。从您的活动开始您的服务。然后,即使关闭应用后,他们仍会继续运行。

相关问题