2011-11-01 14 views
5

Lo so, ci sono un sacco di questi qui, ma ho cercato soluzioni tutto il giorno e non ho ottenuto da nessuna parte. Né l'esempio sui documenti di Google, né nessuno degli altri 5 modi in cui ho trovato qui hanno funzionato per me. Come nel caso tipico, quando faccio clic sulla notifica si chiude la barra di stato e non viene mostrato nulla di nuovo sullo schermo. Sto creando la notifica da un servizio e ho bisogno della notifica per attivare una nuova attività che non è stata ancora creata. Avrò anche bisogno di un modo per passare informazioni a quell'attività tramite l'intento.avvia l'attività dal servizio quando si fa clic sulla notifica

E sì ... questo è Java per Android

Si riportano di seguito i resti frantumati del mio codice.

package com.bobbb.hwk2; 

import java.io.FileOutputStream; 

import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.app.Service; 
import android.content.ContentResolver; 
import android.content.Context; 
import android.content.Intent; 
import android.database.Cursor; 
import android.net.Uri; 
import android.os.IBinder; 
import android.provider.ContactsContract; 
import android.widget.Toast; 

public class contactBackup extends Service 
{ 
    private NotificationManager nManager; 
    private static final int NOTIFY_ID = 1100; 

    @Override 
    public void onCreate() 
    { 
     super.onCreate(); 

     String ns = Context.NOTIFICATION_SERVICE; 
     nManager = (NotificationManager)getSystemService(ns); 
    } 

    @Override 
    public void onStart(Intent intent, int startId) { 
     super.onStart(intent, startId); 

     // inform user that service has started 
     Toast.makeText(getApplicationContext(), R.string.service_started,Toast.LENGTH_LONG).show(); 

     String data = lookUpContacts(); 
     if(saveToSDCard(getResources().getString(R.string.backup_file_name),data)) 
     { 
      Context context = getApplicationContext(); 

      // create the statusbar notification 
      Intent nIntent = new Intent(this,contactViewer.class);//Intent nIntent = new Intent(Intent.ACTION_MAIN); 
      nIntent.setClass(context,contactViewer.class); 
      //nIntent.putExtra("data",data); 


      Notification msg = new Notification(R.drawable.icon,"All contacts records have been written to the file.",System.currentTimeMillis()); 
      // start notification 
      //PendingIntent pIntent = PendingIntent.getService(getApplicationContext(),0,nIntent,PendingIntent.FLAG_UPDATE_CURRENT|Intent.FLAG_FROM_BACKGROUND); 
      PendingIntent pIntent = PendingIntent.getActivity(this,0,nIntent,0); 
      msg.flags = Notification.FLAG_AUTO_CANCEL; 
      msg.setLatestEventInfo(context, 
            "success", 
            "All contacts records have been written to the file.", 
            pIntent); 
      nManager.notify(NOTIFY_ID,msg); 


     } 



    } 

    @Override 
    public void onDestroy() 
    { 
     nManager.cancel(NOTIFY_ID); 
     super.onDestroy(); 
    } 

    @Override 
    public IBinder onBind(Intent intent) 
    { 
     return null; 
    } 

    // function returns string containing information 
    // from contacts 
    public String lookUpContacts() 
    { 
     ... 
    } 

    public boolean saveToSDCard(String fileName, String data) 
    { 
     ... 
    } 

} 

posso solo sperare che tutto ciò che sta causando il mio problema è qualcosa di risolvibile e non più dei difetti folli Sono stato sempre con Eclipse (che nessun altro sembra di aver mai visto>: U)

Se puoi aiutarmi a risolvere questo problema, per favore condividi. Se non si può fare con questo problema specifico, ma sento obbligato a dire cose non collegati sulla pubblicazione, stili, temi, o di buona pratica, allora non

Grazie: D

risposta

6

Edit:

si sta andando ad avere per aggiungere un bandiera per FLAG_ACTIVITY_NEW_TASK:

nIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

Questo perché si sta lanciando da outsi dalla tua app (dalla barra di notifica del sistema).

+0

bene , non è proprio come usi putExtra ... Ma sulla base del tuo consiglio ho provato addi ng che indica sia Intent che PendingIntent e non ha fatto alcuna differenza. Grazie comunque. – user980058

+0

la tua modifica corrisponde a quello che ho fatto. ancora nessun dado: 3 – user980058

0

Questo è quello che succede quando le persone si sovraccaricano. XD L'unico motivo per cui nessuna delle esercitazioni che ho provato a lavorare è perché ho scritto male il nome della mia attività nel manifest. Grazie per l'arresto da

0

Basta aggiungere in seguito a ContactBackup (classe di servizio),

Intent nIntent = new Intent(this,contactViewer.class);//Intent nIntent = new Intent(Intent.ACTION_MAIN); 
     nIntent.setClass(context,contactViewer.class); 
     nIntent.putExtra("data",data); 

    nIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     Notification msg = new Notification(R.drawable.icon,"All contacts records have been written to the file.",System.currentTimeMillis()); 
     // start notification 
     //PendingIntent pIntent = PendingIntent.getService(getApplicationContext(),0,nIntent,PendingIntent.FLAG_UPDATE_CURRENT|Intent.FLAG_FROM_BACKGROUND); 
     PendingIntent pIntent = PendingIntent.getActivity(this,0,nIntent,0); 
     msg.flags = Notification.FLAG_AUTO_CANCEL; 
     msg.setLatestEventInfo(context, 
           "success", 
           "All contacts records have been written to the file.", 
           pIntent); 
     nManager.notify(NOTIFY_ID,msg); 

poi ottenere il valore in classe contactViewer,

come,

String s=getIntent().getStringExtra("data"); 
Problemi correlati