Search code examples
androidandroid-intentserviceandroid-manifestalarmmanager

Unable to start service Intent { flg=0x4 cmp=com.org.sample/.TipActivity (has extras) } U=0: not found


I know lots of people got this issue, but I tried all stack overflow suggestions.

  1. Making Package name small

  2. Verifying service in Manifest and more but still I am not able to resolve this.

  3. Application clean and build.

But nothing worked. Onclicking startservice button, the service started, alarm set and intent not invoking. Getting unable to start service intent.

Here is my code.

MainActivity.java

public class MainActivity extends AppCompatActivity {

    Button btnStartService;
    Button btnStopService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnStartService = (Button)findViewById(R.id.btn_startService);
        btnStopService = (Button)findViewById(R.id.btn_stopService);

        btnStartService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startService();
            }
        });

        btnStopService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stopService();
            }
        });
    }

    // Method to start the service
    public void startService() {
        startService(new Intent(getBaseContext(), Backgroundservice.class));
    }

    // Method to stop the service
    public void stopService() {
        stopService(new Intent(getBaseContext(), Backgroundservice.class));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Background Service

public class Backgroundservice extends Service {

    private AlarmManager alarmManager;
    private PendingIntent pendingIntent;

    public Backgroundservice() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
      public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this, "Service Started!", Toast.LENGTH_LONG).show();
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date = new Date();
        System.out.println("*****" + dateFormat.format(date));
        startAlarm();
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        stopAlarm();
        Toast.makeText(this,"Service Stopped!",Toast.LENGTH_LONG).show();
    }

    public void startAlarm()
    {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(calendar.HOUR_OF_DAY,13);
        calendar.set(calendar.MINUTE,20);
        calendar.set(Calendar.SECOND,10);

        alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(this,TipActivity.class);
        pendingIntent = PendingIntent.getService(this,0, intent, 0);

        alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
        Toast.makeText(this, "Alarm Set", Toast.LENGTH_SHORT).show();
    }

    public void stopAlarm(){

        if (alarmManager != null)
        {
            alarmManager.cancel(pendingIntent);
            Toast.makeText(this, "Alarm Cancelled", Toast.LENGTH_SHORT).show();
        }
    }

}

TipActivity,java

public class TipActivity extends AppCompatActivity {

    Button btnDismiss;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tip);

        btnDismiss = (Button) findViewById(R.id.dismiss);

        btnDismiss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_tip, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.org.sample" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service
            android:name=".Backgroundservice"
            android:enabled="true"
            android:exported="true" >
        </service>

        <activity
            android:name=".TipActivity"
            android:label="@string/title_activity_tip"
            >
        </activity>
    </application>

</manifest>

I worked alot on this to solve the issue but in vain.

Your help is really appreciated.


Solution

  • Replce PendingIntent.getService with PendingIntent.getActivity().