现在的位置: 首页 > 综合 > 正文

android in practice_Creating notifications(portfolio project)

2017年12月24日 ⁄ 综合 ⁄ 共 6966字 ⁄ 字号 评论关闭

Having the caching data in the background Service allows that Service to do other things with that data. A common example of this is to create notifications based on the data that’s retrieved from the server.

Toasts are often used by an Activity to alert the user to an event, but they can also be launched from a Service. Toasts are designed to display information to the user—
they’re not interactive. To get the kind of interactivity we desire, we need to use an android.app.Notification. A Notification allows the user to interact with your application by wrapping an Intent. It can be displayed on the status bar, create a sound, vibrate
the phone, and even trigger custom colored flashing LEDs.

add member of the class PortfolioManagerService:

private static final String TAG = "PortfolioManagerService";
// This is a data access object used for persisting stock information.
private StocksDb db;
// Timestamp of last time stock data was downloaded from the Itnernet
private long timestamp = 0L;
// How old downloaded stock data can be and still be used
private static final int MAX_CACHE_AGE = 15*60*1000; // 15 minutes
// Types of Notifications
private static final int HIGH_PRICE_NOTIFICATION = 1;
private static final int LOW_PRICE_NOTIFICATION = 0;

@Override
public void onCreate(){
super.onCreate();
db=new StocksDb(this);
}

@Override
public void onDestroy(){
super.onDestroy();
db.close();
}

add function checkForAlerts(stocks) in the updateStockData in the Service class PortfolioManagerService:

private void updateStockData(List<Stock> stocks){
// existing code omitted
checkForAlerts(stocks);
}

private void checkForAlerts(Iterable<Stock> stocks){
try{
for(Stock stock:stocks){
double current=stock.getCurrentPrice();
if(current>stock.getMaxPrice()){
createHighPriceNotification(stock);
continue;
}
if(current<stock.getMinPrice()){
createLowPriceNotification(stock);
}
}
}finally{
//AlarmReceiver.releaseLock();
this.stopSelf();
}
}

create Creating  price Notification:

private void createHighPriceNotification(Stock stock) {
NotificationManager mgr=(NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
int dollarBill=R.drawable.dollar_icon;
String shortMsg="High price alert:"+stock.getSymbol();
long time=System.currentTimeMillis();
Notification n=new Notification(dollarBill,shortMsg,time);
String title=stock.getName();
String msg = "Current price $" + stock.getCurrentPrice() + " is high";
Intent intent=new Intent(this,NotificationDetails.class);
intent.putExtra("stock", stock);
PendingIntent pi=PendingIntent.getActivity(this, 0, intent, 0);
n.setLatestEventInfo(this, title, msg, pi);
n.defaults|=Notification.DEFAULT_SOUND;
long[] steps={0, 500, 100, 200, 100, 200};
n.vibrate=steps;
n.ledARGB = 0x80009500;
n.ledOnMS = 250;
n.ledOffMS = 500;
n.flags |= Notification.FLAG_SHOW_LIGHTS;
mgr.notify(HIGH_PRICE_NOTIFICATION, n);
}
private void createLowPriceNotification(Stock stock){
NotificationManager mgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int dollarBill = R.drawable.dollar_icon;
String shortMsg = "Low price alert: " + stock.getSymbol();
long time = System.currentTimeMillis();
Notification n = new Notification(dollarBill, shortMsg, time);

String pkg=getPackageName();
RemoteViews view=new RemoteViews(pkg,R.layout.notification_layout);
String msg = "Current price $" + stock.getCurrentPrice() + " is low";
view.setTextViewText(R.id.notification_message, msg);
n.contentView=view;
Intent i=new Intent(this,NotificationDetails.class);
i.putExtra("stock", stock);
PendingIntent pi=PendingIntent.getActivity(this, 0, i, 0);
n.contentIntent=pi;
n.defaults |= Notification.DEFAULT_SOUND;
long[] steps = {0, 500, 100, 500, 100, 500, 100, 500};
n.vibrate = steps;
n.ledARGB = 0x80A80000;
n.ledOnMS = 1;
n.ledOffMS = 0;
n.flags |= Notification.FLAG_SHOW_LIGHTS;
mgr.notify(LOW_PRICE_NOTIFICATION, n);
}

we’re creating it from our background Service, which is running in a separate process from whatever application that the user is currently viewing.Android has the RemoteViews class to deal with this situation. It only
needs the package name of our application and an XML view B to inflate the View.

The familiar findViewById method is only available from an Activity, not from a Service.we used the setTextViewText method to set the text value of the message that will be shown in our Notification.Also note that we
needed to set the contentIntent of the Notification as well.We didn’t have to do this in the setHighPriceNotification method because we used the setLastEventInfo method that took care of this for us.

PendingIntent—an Intent that will be activated sometime in the future.

Custom XML layout used for a Notification:notification_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/notification_layout_root"
  android:orientation="horizontal" 
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:padding="5dp">
    <ImageView android:id="@+id/notification_icon_left"
               android:layout_width="wrap_content"
               android:layout_height="fill_parent"
               android:layout_marginRight="5dp"
               android:src="@drawable/radioactive_icon"
               />  
    <TextView android:id="@+id/notification_message"
              android:layout_width="wrap_content"
              android:layout_height="fill_parent"
              android:textColor="#000"
              />
    <ImageView android:id="@+id/notification_icon_right"
               android:layout_width="wrap_content"
               android:layout_height="fill_parent"
               android:layout_marginLeft="5dp"
               android:src="@drawable/radioactive_icon"
               />
</LinearLayout>

also we need to create the class NotificationDetails  :

public class NotificationDetails extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.details);

Stock stock = (Stock) this.getIntent().getParcelableExtra("stock");
TextView nameLabel = (TextView) findViewById(R.id.name);
if (stock == null){
nameLabel.setText("No stock passed in to display");
return;
}

nameLabel.setText(stock.getName() + "(" + stock.getSymbol() + ")");

double current = stock.getCurrentPrice();
TextView currentLabel = (TextView) findViewById(R.id.current);
currentLabel.setText("Current Price: $" + current);

TextView minLabel = (TextView) findViewById(R.id.min);
if (current < stock.getMinPrice()){
minLabel.setText("Current price is less than minimum price $" + 
stock.getMinPrice());
minLabel.setTextColor(0xFFFF0000);
} else {
minLabel.setText("Minimum price: $" + stock.getMinPrice());
}

TextView maxLabel = (TextView) findViewById(R.id.max);
if (current > stock.getMaxPrice()){
maxLabel.setText("Current price is more than maximum price $" + 
stock.getMaxPrice());
maxLabel.setTextColor(0xFF00FF00);
} else {
maxLabel.setText("Maximum price: $" + stock.getMaxPrice());
}
}
}

create the file details.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" 
  android:orientation="vertical">
  <TextView 
      android:id="@+id/name" 
      android:layout_width="fill_parent"
   android:layout_height="wrap_content"/>
  <TextView 
      android:id="@+id/current" 
      android:layout_width="fill_parent"
   android:layout_height="wrap_content"/>
  <TextView 
      android:id="@+id/min" 
      android:layout_width="fill_parent"
   android:layout_height="wrap_content"/>
   <TextView 
       android:id="@+id/max" 
       android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>
   

</LinearLayout>

抱歉!评论已关闭.