0
我知道很多人都遇到過這個問題,但我嘗試了所有的堆棧溢出建議。無法啓動服務Intent {flg = 0x4 cmp = com.org.sample/.TipActivity(has extras)} U = 0:未找到
使封裝名小
在清單驗證的服務和更多的,但仍然我不能夠解決這個問題。
應用程序清潔和構建。
但沒有任何工作。點擊startservice按鈕,服務啓動,警報設置和意圖不調用。無法啓動服務意圖。
這是我的代碼。
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>
我在這方面做了很多工作以解決問題,但徒勞無功。
您的幫助真的很感謝。
問題已解決。我叫PendingIntent.getService而不是PendingIntent.getActivity()。抱歉。謝謝 – user2350138