2013-08-01 27 views
2
public class FinishService extends Service{ 

private NotificationManager mNM; 
public static String ChangeDate; 
public static Context mContext; 
public static String today; 



private int NOTIFICATION = R.string.local_service_started; 
private int START_STICKY; 


public class LocalBinder extends Binder { 
    FinishService getService() { 
      return FinishService.this; 
     } 
    } 




@Override 
public void onCreate() { 
    mContext=FinishService.this; 
     mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); 
     showNotification(); 
} 



    @SuppressLint("Override") 
     public int onStartCommand(Intent intent, int flags, int startId) { 
      Log.i("LocalService", "Received start id " + startId + ": " + intent); 
      Toast.makeText(this, "Service start", Toast.LENGTH_SHORT).show(); 

      // We want this service to continue running until it is explicitly 
      // stopped, so return sticky. 
      return START_STICKY; 
     } 



@Override 
public IBinder onBind(Intent intent) { 
    // TODO Auto-generated method stub 
    return mBinder; 
} 

//@Override 
public void onDestroy() { 

      if(AstrobixStaticMethod.isInternetAvailable(FinishService.this)){ 
       Date date = Calendar.getInstance().getTime(); 
       DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); 
       ChangeDate=formatter.format(date); 
       SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); 
       Calendar cal=Calendar.getInstance(); 
      try{ 
       cal.setTime(sdf.parse(ChangeDate)); 
      }catch(Exception e){ 

      } 
      cal.add(Calendar.DATE, 1); 
      ChangeDate=sdf.format(cal.getTime()); 
      Toast.makeText(this, ""+ChangeDate, Toast.LENGTH_SHORT).show(); 
      Log.e("Next Date", ChangeDate); 
      RahuActivity.nextAlaram(); 

      } 
      else{ 
       Intent mIntent=new Intent(getApplicationContext(),Information.class); 
       mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
       mContext.startActivity(mIntent); 


      } 


} 



private final IBinder mBinder = new LocalBinder(); 

@SuppressWarnings("deprecation") 
    private void showNotification() { 
     // This is the 'title' of the notification 
     CharSequence title = "Rahukaal has finished.!!"; 
     // This is the icon to use on the notification 
     int icon = R.drawable.ic_launcher; 
     // This is the scrolling text of the notification 
     //CharSequence text =RahuParserClasses.mList.get(0); 
     CharSequence text ="Rahukaal Time Finished Now"; 
     // What time to show on the notification 
     long time = System.currentTimeMillis(); 
     Notification notification = new Notification(icon, text, time); 
     // The PendingIntent to launch our activity if the user selects this notification 
     Intent notificationIntent = new Intent(); 
    // PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, RahuTimeActivity.class), 0); 
     PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 
     // Set the info for the views that show in the notification panel. 
     notification.setLatestEventInfo(this, title, text, contentIntent); 
     // Clear the notification when it is pressed 
     notification.flags |= Notification.FLAG_AUTO_CANCEL; 
     //Vibrates fon on notification 
     notification.vibrate=new long[] { 100, 250, 100, 500 }; 
     notification.defaults |= Notification.DEFAULT_SOUND; 
     //Uri defaultRingtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     // Send the notification to the system. 

     mNM.notify(NOTIFICATION, notification); 


     stopSelf(); 

    } 

任何人都可以幫助我。我做了一個應用程序,我從匹配時間設置了警報。 當時間匹配服務類正在運行,但我已編碼銷燬方法 如果互聯網不可用phone.it將轉到BroadcastReceiver類,但它 已消失,並顯示應用程序未打開時出現錯誤。如何打電話給服務類別的活動

Loacat錯誤的位置:

java.lang.RuntimeException: Unable to stop service [email protected]: android.content.ActivityNotFoundException: Unable to find explicit activity class { com.astrobix.astrobixrahukaal/com.astrobix.service.Information}; have you declared this activity in your AndroidManifest.xml? 
+1

其實你想達到什麼樣的? - 如果互聯網不可用,您希望運行廣播接收器?你想要嗎?其實你的問題有點混亂。 :( –

+0

astrobixrahukaal - 這是你的活動名稱嗎? –

+0

@ Jashan:-ya其我的項目名稱不是活動名稱,我在那裏聲明活動名稱是RahuActivity的未決意圖。 – FarhaSameer786

回答

2

問題是你不能使用在廣播接收機startActivity方法試試這一個。

`Intent mIntent=new Intent(FinishService.this,Information.class); 

sendBroadcast(mIntent);`

+1

@ Jashan-請給我投票請求 – FarhaSameer786

+1

哦天啊!所以你發佈一個問題來建立一個配置文件?這很傷心,很不專業。永遠不要在SO上投票。好。我已經投了你的問題了。我不能爲評論投票。 :(因爲你的意見沒有任何用處,你可以發表你自己的答案,我會爲你投票。 –

+1

@ Jashan-這是我的問題,我發佈了http://stackoverflow.com/問題/ 17991116 /如何對呼叫的活動,從服務類?noredirect = 1#comment26306314_17991116謝謝我需要一個frnd,因爲stackflow阻塞了我的最後一個id它我沒有20以上的聲譽,這就是爲什麼 – FarhaSameer786

1

錯誤解釋您的問題

你宣佈你的AndroidManifest.xml這個活動?

您的活動添加到AndroidManifest應用標籤內:

<activity android:name=".newActivity"> 
+0

- 但從我在哪裏打電話課是我的課程課以及我通過意圖發送的地方是BroadcastReceive r類,並且兩個類都在Manifest.xml文件中提到 – FarhaSameer786

+0

@ FarhaSameer786您確定此活動使用了正確的包嗎?發佈整個logcat消息。 –

+0

@ Marcin Gawel-ya先生非常確定... – FarhaSameer786

0

試試這個:

Intent mIntent=new Intent(mContext,Information.class); 
      mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      mContext.startActivity(mIntent); 

無法找到明確的活動類{com.astrobix.astrobixrahukaal/com.astrobix.service 。信息};你有沒有在你的AndroidManifest.xml中聲明這個活動?

have u added in the AndroidManifest? 
+0

-mContext被引用到哪個類...亞我已經在清單文件中添加了服務類和broadcastreceiver – FarhaSameer786

+0

您已在onCreate() –