2011-11-01 4 views
5

나는 여기에 이들 중 많은 톤이 있지만, 하루 종일 솔루션을 시도하고 어디서나 얻지 못했습니다. Google의 문서에 대한 예나 여기에서 찾은 다른 5 가지 방법 중 하나도 나를 위해 전혀 효과가 없었습니다. 일반적인 경우와 마찬가지로 알림을 클릭하면 상태 표시 줄이 닫히고 화면에 아무 것도 표시되지 않습니다. 서비스에서 알림을 만들고 있는데 아직 생성되지 않은 새 활동을 시작하라는 알림이 필요합니다. 의도를 통해 해당 활동에 정보를 전달할 방법이 필요합니다.알림을 클릭 할 때 서비스에서 시작 활동

는 그리고 네 ...이 내 코드의 부서진 잔해 다음과 같다 무엇 안드로이드

에 대한 자바입니다.

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) 
    { 
     ... 
    } 

} 

내 문제를 일으키는 어떤 희망 만 할 수는 고칠 수 뭔가 내가 이클립스지고있어 미친 결함이 아닌 더 (지금까지 본을 다른 사람이 보인다> : U)

이 문제를 해결하는 데 도움을 주실 수 있으면 공유하십시오. 는

감사합니다 NOT이 특정 문제에 도움이되지만 게시, 스타일, 주제, 또는 좋은 연습에 대한 관계없는 말을 할 의무감을 느낄 수없는 경우, 다음을 수행하십시오 D

답변

6

편집 :

당신은 FLAG_ACTIVITY_NEW_TASK에 대한 플래그를 추가해야 할거야 :

nIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

당신이 outsi에서 시작하고 있기 때문입니다 귀하의 애플 리케이션 (시스템 알림 표시 줄에서).

+0

아니라 , 그건 당신이 putExtra를 사용하는 방법이 아니에요 ...하지만 조언을 기반으로 나는 addi 그 플래그를 의도와 PendingIntent에 모두 적용하여 차이를 만들지 마십시오. 그래도 고마워. – user980058

+0

편집 내용이 내가 한 내용과 일치합니다. 여전히 주사위가 없습니다 : 3 – user980058

0

이것은 사람들이 스스로 과로 할 때 발생합니다. XD 내가 피곤한 튜토리얼을 다룰 수있는 유일한 이유는 매니페스트에서 내 활동 이름의 철자를 잘못 입력했기 때문입니다.

에 의해 정지 감사
0

그냥

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

다음과 같은

, contactViewer 클래스의 값을 얻을 contactBackup (서비스 클래스)에 다음을 추가,

String s=getIntent().getStringExtra("data"); 
관련 문제