通知未显示在通知栏上。尝试了一切

时间:2017-07-24 12:08:02

标签: android

我试图在这里做一个简单的通知;

  

按下按钮,通知显示在通知栏的顶部

但是当我按下按钮时没有任何反应;

  

没有错误,没有应用程序崩溃,顶部没有通知。

我无法弄清楚这里发生了什么。请帮忙。

public class MainActivity extends AppCompatActivity {

private Button btn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



}

public void fly(View v)
{
    NotificationCompat.Builder tame = new NotificationCompat.Builder(this);
    tame.setSmallIcon(R.mipmap.ic_launcher);
    tame.setContentTitle("Notification's here");
    tame.setContentText("SomeBody once told me the world is gonna roll me. ");
    tame.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);


    Intent intent = new Intent(this, Second_activity.class);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addParentStack(Second_activity.class);
    stackBuilder.addNextIntent(intent);

    PendingIntent pendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
    tame.setContentIntent(pendingIntent);

    NotificationManager mg = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mg.notify(0,tame.build());



}

这是我的xml代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.apple.notification.MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:id="@+id/textView" />

<Button
    android:id="@+id/Notify"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="189dp"
    android:text="Button"
    android:onClick="fly"
    />

0 个答案:

没有答案