无法启动服务Intent {flg = 0x4 cmp = com.org.sample / .TipActivity(has extras)} U = 0:未找到

时间:2015-09-27 02:51:22

标签: android android-intent service android-manifest alarmmanager

我知道很多人都遇到了这个问题,但我尝试了所有堆栈溢出建议。

  1. 使包名更小

  2. 在Manifest中验证服务以及更多,但我仍然无法解决此问题。

  3. 应用程序清理和构建。

  4. 但没有任何效果。单击启动服务按钮,服务启动,警报设置和意图不调用。无法启动服务意图。

    这是我的代码。

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

    后台服务

    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>
    

    我在这方面做了很多工作来解决这个问题但是徒劳无功。

    非常感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

使用PendingIntent.getActivity()Replce PendingIntent.getService。