2017-09-22 27 views
0

所以我嘗試推出我的第二個活動是「dummy.class」一旦點擊通知,新的活動不會啓動

爲其代碼是在這裏:

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); 
} 
} 

我想要做到這一點,當我點擊由這個代碼在我的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()); 
} 

這裏是activity_main的XML:

<?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> 

但是,當我點擊通知時,它什麼都不做(除了由於代碼導致本身無效)。我只是在學習,所以我不知道自己做錯了什麼,可以使用另一雙眼睛。

回答

1

確定這是個愚蠢的錯誤就像我以爲,我忘了給新的類添加到清單文件作爲一項活動,只要加入這個固定:

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