在通知中显示动态数据

时间:2015-05-27 05:32:21

标签: android notifications

我是Android新手。我想在通知栏中显示真实数据(TrafficStats)。我为此制作了自定义通知栏,但我无法在通知中显示动态数据。谁知道请帮助我。

这是我的通知XML。

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativelayout_noti"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<RelativeLayout
    android:id="@+id/relativelayout_image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="5dp"
    android:layout_marginTop="10dp" >

    <ImageView
        android:id="@+id/image_icon"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:src="@drawable/icon" />
  </RelativeLayout>

  <LinearLayout
    android:id="@+id/linearlayout_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="10dp"
    android:layout_toRightOf="@id/relativelayout_image"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textview_mobile"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Mobile"
        android:textColor="@color/white" />

    <TextView
        android:id="@+id/textview_wifi"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:text="WiFi"
        android:textColor="@color/white" />
  </LinearLayout>

  <LinearLayout
    android:id="@+id/linearlayout_mobiledata"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="10dp"
    android:layout_toRightOf="@+id/linearlayout_text"
    android:background="@color/fb_blue"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView_mobiledata"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:text=" MB USED THIS MONTH"
        android:textColor="@color/white" />
 </LinearLayout>

 <LinearLayout
    android:id="@+id/linearlayout_wifidata"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/linearlayout_mobiledata"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="5dp"
    android:layout_toRightOf="@+id/linearlayout_text"
    android:background="@color/fb_blue"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView_wifidata"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:text=" MB USED THIS MONTH"
        android:textColor="@color/white" />
 </LinearLayout>

</RelativeLayout>

这是我使用通知的主要活动。

public class MainActivity extends Activity {

private TextView tvDataUsageWiFi, tvDataUsageMobile, tvDataUsageTotal;

private long dataUsageTotalLast = 0;

 ArrayAdapter<ApplicationItem> adapterApplications;

  ImageView image_icon;
  TextView mobile, wifi;

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

    int icon = R.drawable.icon;
    long when = System.currentTimeMillis();
    @SuppressWarnings("deprecation")
     Notification notification = new Notification(icon, "", when);

    NotificationManager mNotificationManager = (NotificationManager)  getSystemService(NOTIFICATION_SERVICE);

    RemoteViews contentView = new RemoteViews(getPackageName(),
            R.layout.notification_view);
    contentView.setImageViewResource(R.id.icon, R.drawable.icon);
    contentView.setTextViewText(R.id.textview_mobile, "MOBILE");
    contentView.setTextViewText(R.id.textview_wifi, "WIFI");
    contentView.setTextViewText(R.id.textView_mobiledata,
            " MB USED THIS MONTH");
    contentView.setTextViewText(R.id.textView_wifidata,
            " MB USED THIS MONTH");

    notification.contentView = contentView;

    Intent notificationIntent = new Intent(this, Main.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            notificationIntent, 0);
    notification.contentIntent = contentIntent;

    notification.flags |= Notification.FLAG_ONGOING_EVENT;
    mNotificationManager.notify(1, notification);

    tvDataUsageWiFi = (TextView) findViewById(R.id.tvDataUsageWiFi);
    tvDataUsageMobile = (TextView) findViewById(R.id.tvDataUsageMobile);
    tvDataUsageTotal = (TextView) findViewById(R.id.tvDataUsageTotal);

    if (TrafficStats.getTotalRxBytes() != TrafficStats.UNSUPPORTED
            && TrafficStats.getTotalTxBytes() !=  TrafficStats.UNSUPPORTED) {
        handler.postDelayed(runnable, 0);

    } else {
    }
    }

    public Handler handler = new Handler();
    public Runnable runnable = new Runnable() {
    public void run() {
        long mobile = TrafficStats.getMobileRxBytes()
                + TrafficStats.getMobileTxBytes();
        long total = TrafficStats.getTotalRxBytes()
                + TrafficStats.getTotalTxBytes();
        tvDataUsageWiFi
                .setText("" + (total - mobile) / 1024 / 1000 + " MB");
        tvDataUsageMobile.setText("" + mobile / 1024 / 1000 + " MB");
        tvDataUsageTotal.setText("" + total / 1024 / 1000 + " MB");
        if (dataUsageTotalLast != total) {
            dataUsageTotalLast = total;
            updateAdapter();
        }
        handler.postDelayed(runnable, 5000);
      }

      private void updateAdapter() {
        // TODO Auto-generated method stub

      }
   };

 }

1 个答案:

答案 0 :(得分:1)

您需要运行后台的服务并更新远程查看时间和时间。 service链接。更新删除视图,notificationmanager.notify具有相同的ID。实际上你可以使用activity来更新远程视图,但更好的是后台服务。