我创建了一个活动,我在其中创建了一个进度条,如下所示,我已经编写了通知但它无效。通知有时会出现在屏幕的上角。我希望将通知作为屏幕中间的消息框。怎么实现呢?我的活动代码如下:
public class oddg extends Activity implements OnClickListener
{
ProgressDialog dialog;
int increment;
int maximum ;
private NotificationManager mManager;
private static final int APP_ID = 0;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
Button startbtn = (Button) findViewById(R.id.startbtn);
startbtn.setOnClickListener(this);
mManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
}
@Override
public void onClick(View arg0)
{
// get the increment value from the text box
EditText et = (EditText) findViewById(R.id.increment);
// convert the text value to a integer
increment = Integer.parseInt(et.getText().toString());
dialog = new ProgressDialog(this);
dialog.setCancelable(true);
dialog.setMessage("Loading...");
// set the progress to be horizontal
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
// reset the bar to the default value of 0
dialog.setProgress(0);
// get the maximum value
EditText max = (EditText) findViewById(R.id.maximum);
// convert the text value to a integer
maximum = Integer.parseInt(max.getText().toString());
// set the maximum value
dialog.setMax(maximum);
// display the progressbar
dialog.show();
// create a thread for updating the progress bar
Thread background = new Thread (new Runnable() {
public void run()
{
try
{
// enter the code to be run while displaying the progressbar.
// This example is just going to increment the progress bar:
// So keep running until the progress value reaches maximum value
while(dialog.getProgress()<= dialog.getMax())
{
// wait 500ms between each update
Thread.sleep(500);
// active the update handler
progressHandler.sendMessage(progressHandler.obtainMessage());
}
}
catch (java.lang.InterruptedException e)
{
// if something fails do something smart
}
}
});
// start the background thread
background.start();
}
// handler for the background updating
Handler progressHandler = new Handler()
{
public void handleMessage(Message msg)
{
if(dialog.getProgress()== dialog.getMax())
{
Notification notification = new Notification(R.drawable.icon,"Click here", System.currentTimeMillis());
notification.setLatestEventInfo(oddg.this,"App Name","Description of the notification",PendingIntent.getActivity(getApplicationContext(), CONTEXT_IGNORE_SECURITY, getIntent(), CONTEXT_IGNORE_SECURITY));
}
dialog.incrementProgressBy(increment);
}
};
答案 0 :(得分:1)
就像这样
notificationMgr = context.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0,
new Intent(ctx, myActClassName.class), 0);
Notification notification = new Notification(R.drawable.icon, someText, System.currentTimeMillis());
notification.setLatestEventInfo(ctx, title, contentText, contentIntent);
int HELLO_ID = 10;
notificationManager.notify(HELLO_ID, notification);
状态栏通知检查此
http://developer.android.com/guide/topics/ui/notifiers/notifications.html
http://javainnovations.blogspot.com/2009/05/how-to-send-message-to-status-bar-in.html