2016-06-15 5 views
0

Я пытаюсь создать специальное уведомление с ImageButton. Когда onclicked, ImageButton должен начать действие с именем TaskActivity, но я не могу этого добиться.ImageButton не работает над пользовательским уведомлением - Android

Ниже приведены мои коды:

MainActivity.java

общественного недействительными OnCreate (Bundle savedInstanceState) {

//button intents 
    Intent cmdIntent = new Intent(
         MainActivity.this, cmdButtonListener.class); 
    PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(
             MainActivity.this, 0, cmdIntent, 0); 

    //notification mgr 
    int notifyID = 001; 
    String ns = Context.NOTIFICATION_SERVICE; 
    NotificationManager mNotifyMgr = (NotificationManager)getSystemService(ns); 

    //new notification 
    int icon = R.drawable.icon3; 
    long when = System.currentTimeMillis(); 
    @SuppressWarnings({ "deprecation" }) 
    Notification notify = new Notification(icon,getString(R.string.text),when); 

    //remote views 
    RemoteViews contentView = new RemoteViews(getPackageName(),R.layout.custom_notification); 
    contentView.setImageViewResource(R.id.notification_image, R.drawable.icon4); 
    contentView.setTextViewText(R.id.notification_title, "APP TITLE"); 
    contentView.setTextViewText(R.id.notification_text, "hello"); 
    contentView.setOnClickPendingIntent(R.id.notification_image, pendingSwitchIntent); 
    notify.contentView = contentView; //set 

    //notification intent 
    Intent nIntent = new Intent(MainActivity.this,MainActivity.class); 
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, nIntent, 0); 

    notify.contentIntent = contentIntent; 

    mNotifyMgr.notify(notifyID,notify); 

}

общественного класса cmdButtonListener расширяет BroadcastReceiver {

@Override 
    public void onReceive(Context context, Intent intent){ 
     System.out.println("Here, I am here"); 
     Intent newAct = new Intent(MainActivity.this, TaskActivity.class); 
     startActivity(newAct); 

    } 

}

Android Manifest

<receiver android:name=".MainActivity$cmdButtonListener" /> 

Я не знаю, где я ошибаюсь, так как я делаю то, что показано в большинстве обучающих онлайн. :(

ответ

0

Наконец я нашел решение ...

Объявить ниже переменной

private static Context mContext; 

Добавить ниже код внутри общественного ничтожной OnCreate (Bundle savedInstanceState) {

mContext = this; 

Наконец, измените приведенный ниже код кода на открытый класс cmdButtonListener расширяет BroadcastReceiver {

Intent newAct = new Intent(MainActivity.this, TaskActivity.class); 
startActivity(newAct); 

Для

Intent Act = new Intent(mContext,TaskActivity.class); 
mContext.startActivity(Act);