【问题标题】:Creating notification in android app在android应用程序中创建通知
【发布时间】:2015-04-06 02:16:06
【问题描述】:

我正在创建我的第一个 android 应用程序,我需要一些帮助来使用按钮创建一个简单的通知。按下按钮后,我希望在设定的时间后显示带有自定义消息的通知。无论用户在做什么,我都希望通知出现。非常感谢

【问题讨论】:

    标签: java android notifications


    【解决方案1】:

    在android中有一种叫toast方法的方法。

    在你的按钮点击事件中

    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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-23
      • 1970-01-01
      相关资源
      最近更新 更多