如何从服务类调用Activity

时间:2013-08-01 10:11:15

标签: android service

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

    }

任何人都可以帮助我。我做了一个应用程序,我从匹配时间设置警报。 当时间匹配服务类正在运行但我已经编写了destroy方法 如果在手机上没有互联网,那么它将转到BroadcastReceiver类 当应用程序未打开时,它会消失并显示错误。

这里的Loacat错误:

 java.lang.RuntimeException: Unable to stop service   com.astrobix.service.FinishService@40523370: 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?

3 个答案:

答案 0 :(得分:2)

问题是您无法在广播接收器上使用 startActivity 方法尝试使用此方法。

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

sendBroadcast(mIntent);`

答案 1 :(得分:1)

错误解释了您的问题

  

您是否在AndroidManifest.xml中声明了此活动?

将您的活动添加到Application标记内的AndroidManifest:

<activity android:name=".newActivity">

答案 2 :(得分:0)

试试这个:

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

无法找到显式活动类{com.astrobix.astrobixrahukaal / com.astrobix.service.Information};你有没有在AndroidManifest.xml中声明这个活动?

have u added in the AndroidManifest?