简体   繁体   中英

Upon clicking notification, new activity doesn't start

So I am trying to launch my second activity which is "dummy.class"

the code for which is here:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

public class dummy extends AppCompatActivity {

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

I am trying to do this when I click the notification that is generated by this code in my MainActivity:

public class MainActivity extends AppCompatActivity 
{

int notifyId = 1;
int messages = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button b1 = (Button) findViewById(R.id.gen_notif);

}


public void createNotif(View v)
{
    messages += 1;
    notifyId += 1 ;
    NotificationManager notificationManager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    Intent intent=new Intent(MainActivity.this ,dummy.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this,(int) System.currentTimeMillis(),intent,0);
    NotificationCompat.Builder n = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
            .setContentTitle("Hello")
            .setContentText("Hello World Notification")
            .setContentIntent(pendingIntent)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setNumber(messages)
            .setAutoCancel(true);
    notificationManager.notify(notifyId,n.build());
}

Here is the XML for activity_main :

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
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.manan.notif.MainActivity">

<Button
    android:id="@+id/gen_notif"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/b_text"
    android:onClick="createNotif"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

However when I click the notification it does nothing (except dismiss itself due to the code). I'm just learning so I don't know what I'm doing wrong, could use another pair of eyes.

Ok It was a stupid mistake like I thought, I forgot to add the new class to the manifest file as an activity, simply adding this fixed it:

 <activity android:name="dummy"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
        </intent-filter>
    </activity>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM