Java 在android应用程序中创建通知
我正在创建我的第一个android应用程序,我需要一些使用按钮创建简单通知的帮助。按下按钮后,我希望在设定的时间后出现一条带有自定义消息的通知。我希望无论用户在做什么,都会显示通知。非常感谢在android中,有一种方法叫做toast方法 在按钮单击事件中Java 在android应用程序中创建通知,java,android,notifications,Java,Android,Notifications,我正在创建我的第一个android应用程序,我需要一些使用按钮创建简单通知的帮助。按下按钮后,我希望在设定的时间后出现一条带有自定义消息的通知。我希望无论用户在做什么,都会显示通知。非常感谢在android中,有一种方法叫做toast方法 在按钮单击事件中 Toast.makeText(getApplicationContext(), "this is my Toast message!!! =)", Toast.LENGTH_LONG).show();// popup keep lon
Toast.makeText(getApplicationContext(), "this is my Toast message!!! =)",
Toast.LENGTH_LONG).show();// popup keep long time
Toast.makeText(getApplicationContext(), "this is my Toast message!!! =)",
Toast.LENGTH_SHORT).show();//popup keep short time
既然你是乞丐,我将提供完整的代码
//在你的主要活动中
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MyAndroidAppActivity extends Activity {
Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
}
public void addListenerOnButton() {
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//Toast method
Toast.makeText(getApplicationContext(), "this is my Toast message!!! =)",
Toast.LENGTH_LONG).show();
}
});
}
}
//In your res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button - Go to mkyong.com" />
</LinearLayout>
导入android.app.Activity;
导入android.content.Intent;
导入android.net.Uri;
导入android.os.Bundle;
导入android.widget.Button;
导入android.view.view;
导入android.view.view.OnClickListener;
公共类MyAndroidAppActivity扩展活动{
按钮;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
}
public void addListenerOnButton(){
按钮=(按钮)findViewById(R.id.button1);
setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图arg0){
//吐司法
Toast.makeText(getApplicationContext(),“这是我的Toast消息!!!=”,
Toast.LENGTH_LONG).show();
}
});
}
}
//在res/layout/main.xml中