无法启动服务 Intent { flg=0x4 cmp=com.org.sample/.TipActivity (has extras) } U=0: 未找到
Unable to start service Intent { flg=0x4 cmp=com.org.sample/.TipActivity (has extras) } U=0: not found
我知道很多人遇到过这个问题,但我尝试了所有堆栈溢出建议。
使包名称变小
正在清单中验证服务等,但我仍然无法解决这个问题。
清理并构建应用程序。
但没有任何效果。单击启动服务按钮,服务启动,警报设置和意图不调用。无法启动服务意图。
这是我的代码。
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();
}
}
}
提示活动,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()。
我知道很多人遇到过这个问题,但我尝试了所有堆栈溢出建议。
使包名称变小
正在清单中验证服务等,但我仍然无法解决这个问题。
清理并构建应用程序。
但没有任何效果。单击启动服务按钮,服务启动,警报设置和意图不调用。无法启动服务意图。
这是我的代码。
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();
}
}
}
提示活动,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()。