从通知栏中获取通知

时间:2014-05-09 07:14:22

标签: android notifications android-contentprovider android-contentresolver badge

我需要在设备中获取未读通知计数任何已安装的应用

例如:获取Facebook App,Whats app App或设备消息等的未读通知计数,通知栏中存在的尚未读取的任何内容。

据我所知,我需要所有这些应用的内容URI。但由于我不知道所有的URI,我如何继续这一点。有没有办法从通知栏中获取所有通知

1 个答案:

答案 0 :(得分:0)

  This is the sample Notification app hope this will help you

package com.example.sampletest;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity {
    ListView l1;
//  public static int [] prgmImages={R.drawable.rlp, R.drawable.skp, R.drawable.ramesan, R.drawable.ramalingam, R.drawable.sweena};

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  l1 = (ListView)findViewById(R.id.list);
  String[] values = new String[] {"ANDROID","Java","NETWORKING","ORACLE","DOTNET","VISUAL BASIC"};

// Define a new Adapter
// First parameter - Context
// Second parameter - Layout for the row
// Third parameter - ID of the TextView to which the data is written
// Forth - the Array of data

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.list_row, R.id.title, values);


// Assign adapter to ListView
l1.setAdapter(adapter); 
 }

 @Override
protected void onStart() {
    // TODO Auto-generated method stub
    super.onStart();
}


@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    Toast.makeText(getApplicationContext(), "Back to foreground",Toast.LENGTH_SHORT).show();
}

@SuppressWarnings("deprecation")
@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
     createNotification();
     /* NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
      Notification notification = new Notification(R.drawable.ic_launcher,
        "New Message", System.currentTimeMillis());

       Intent notificationIntent = new Intent(this, MainActivity.class);
      PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
        notificationIntent, 0);

       notification.setLatestEventInfo(MainActivity.this,"Message",
        "RUNNING IN BACKGROUND", pendingIntent);
      notificationManager.notify(9999, notification);*/

}

@Override
protected void onStop() {
    // TODO Auto-generated method stub
    super.onStop();
}

@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
}

@SuppressWarnings("deprecation")
private void createNotification() {

    NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    int unique_id = 145558;
    Intent nintent = new Intent();
    nintent.setClass(this, MainActivity.class);

    PendingIntent pin = PendingIntent.getActivity(getApplicationContext(),
            0, nintent, 0);
    String title = "Notification";
    String body = "RUNNING IN BACKGROUND";
    Notification n = new Notification(R.drawable.ic_launcher, body,
            System.currentTimeMillis());
    n.contentIntent = pin;
    n.setLatestEventInfo(getApplicationContext(), title, body, pin);
    n.flags |= Notification.FLAG_AUTO_CANCEL;
    n.defaults = Notification.DEFAULT_ALL;
    nm.notify(unique_id, n);

}

}