android如何在一段时间后在通知栏中显示应用程序图标

时间:2013-06-19 07:53:52

标签: android

我正在使用本教程http://chrisrisner.com/31-Days-of-Android--Day-16%E2%80%93Notifications在notifdication栏中显示应用程序但是在通知栏中继续显示我的图标我希望在特定时间段后显示或提醒。该怎么做?

 public class DaySixteenActivity extends BaseActivity {
private TextView lblTextViewOne;
private EditText editText1;
public static int notificationId = 0;

public static String MyStaticString;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);  
    Log.i("DaySixteenActivity", "onCreate Start");

    lblTextViewOne = (TextView) findViewById(R.id.lblTextViewOne);
    lblTextViewOne.setText(R.string.test_one);

    editText1 = (EditText) findViewById(R.id.editText1);
    editText1.setText(R.string.test_one);



    Button button1 = (Button) findViewById(R.id.button1);
    button1.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            lblTextViewOne.setText(editText1.getText());

            String notificationService = Context.NOTIFICATION_SERVICE;
            NotificationManager notificationManager = 
(NotificationManager) getSystemService(notificationService);            

            Notification notification = new 
 Notification(R.drawable.cherry_icon, 

 "Hello Notification!", System.currentTimeMillis());


            Intent notificationIntent = new  
Intent(getApplicationContext(), DaySixteenActivity.class);
            PendingIntent contentIntent = 
PendingIntent.getActivity(getApplicationContext(), 0,
                                            notificationIntent, 0);
            notification.setLatestEventInfo(getApplicationContext(), 
 "Notification Title", 
                                            "CherryApplication", contentIntent);




            notificationManager.notify(1, notification);
        }
    });     

    Log.i("DaySixTeenActivity", "onCreate End");
}

}


          public class MyApplication extends Application {
private String myApplicationString;

public String getMyApplicationString() {
    return myApplicationString;
}

public void setMyApplicationString(String myApplicationString) {
    this.myApplicationString = myApplicationString;
}       
 }



               public class PassableObject implements Parcelable {
private String myStringValue;

public PassableObject() {}

public PassableObject(Parcel inParcel) {
    myStringValue = inParcel.readString();
}

public int describeContents() {
    return 0;
}




                  public abstract class BaseActivity extends Activity {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.options_menu, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.item1:
        startActivity(new Intent(getApplicationContext(),
                DaySixteenActivity.class));
        //End the current activity
        return true;
    default:
        return super.onContextItemSelected(item);
    }
}
    }


public void writeToParcel(Parcel outParcel, int flags) {
    outParcel.writeString(myStringValue);
}

public String getMyStringValue() {
    return myStringValue;
}

public void setMyStringValue(String myStringValue) {
    this.myStringValue = myStringValue;
}

public static final Parcelable.Creator<PassableObject> CREATOR 
= new Parcelable.Creator<PassableObject>() {
    public PassableObject createFromParcel(Parcel in) {
        return new PassableObject(in);
    }

    public PassableObject[] newArray(int size) {
        return new PassableObject[size];
    }
};
  }

2 个答案:

答案 0 :(得分:1)

你可以使用AlarmManager作为那个 查看这些链接以获取更多信息

Alarm Manager Example


http://developer.android.com/reference/android/app/AlarmManager.html

答案 1 :(得分:0)

放大写通知服务

 notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    Notification notifyDetails=new Notification(
                                                R.drawable.icon,contentTitle,System.currentTimeMillis());

        notificationManager .notify(MY_SIMPLE_NOTFICATION_ID, notifyDetails);

试试这个对我有用..